Skip to content

Instantly share code, notes, and snippets.

@afixoftrix
Created July 8, 2019 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save afixoftrix/f5efeae6229f39fd4eb0787bd257cd7c to your computer and use it in GitHub Desktop.
Save afixoftrix/f5efeae6229f39fd4eb0787bd257cd7c to your computer and use it in GitHub Desktop.
a solution
// 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