Created
July 8, 2019 11:59
-
-
Save afixoftrix/f5efeae6229f39fd4eb0787bd257cd7c to your computer and use it in GitHub Desktop.
a solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// full solution: https://github.com/afixoftrix/odin_basic_info_site | |
const http = require('http'); | |
const fs = require('fs'); | |
const path = require('path') | |
const server = http.createServer((req, res) => { | |
if (req.url === '/'){ | |
const filepath = path.join( process.cwd(), "/public/index.html" ) | |
console.log(filepath); | |
fs.readFile(filepath, (err, html) => { | |
if (err){ | |
console.log(err); | |
return err | |
} | |
res.writeHead(200, { 'Content-Type': 'text/html'}) | |
res.write(html) | |
res.end() | |
}) | |
} | |
else if (req.url === '/about'){ | |
const filepath = path.join( process.cwd(), "/public/about.html" ) | |
console.log(filepath); | |
fs.readFile(filepath, (err, html) => { | |
if (err){ | |
console.log(err); | |
return err | |
} | |
res.writeHead(200, { 'Content-Type': 'text/html'}) | |
res.write(html) | |
res.end() | |
}) | |
} | |
else if (req.url === '/contact-me'){ | |
const filepath = path.join( process.cwd(), "/public/contact-me.html" ) | |
console.log(filepath); | |
fs.readFile(filepath, (err, html) => { | |
if (err){ | |
console.log(err); | |
return err | |
} | |
res.writeHead(200, { 'Content-Type': 'text/html'}) | |
res.write(html) | |
res.end() | |
}) | |
} | |
else if ((req.url.indexOf(".css")) != -1){ | |
const filepath = path.join( process.cwd(), "/public/styles.css" ) | |
console.log(filepath); | |
fs.readFile(filepath, (err, css) => { | |
if (err){ | |
console.log(err); | |
return err | |
} | |
res.writeHead(200, { 'Content-Type': 'text/css'}) | |
res.write(css) | |
res.end() | |
}) | |
} | |
else { | |
const filepath = path.join(process.cwd(), "/public/404.html"); | |
console.log(filepath); | |
fs.readFile(filepath, (err, html) => { | |
if (err) { | |
console.log(err); | |
return err; | |
} | |
res.writeHead(404, { "Content-Type": "text/html" }); | |
res.write(html); | |
res.end(); | |
}); | |
} | |
}) | |
server.listen(3000, () => { console.log("listening on port 3000");}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment