Skip to content

Instantly share code, notes, and snippets.

@RayanBassetti
Created July 4, 2019 09:17
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 RayanBassetti/61b73131cdf27cfd6acd95ef60aa7c82 to your computer and use it in GitHub Desktop.
Save RayanBassetti/61b73131cdf27cfd6acd95ef60aa7c82 to your computer and use it in GitHub Desktop.
Odin - NodeJS Project #1
const http = require('http');
const fs = require('fs');
const url = require('url');
http.createServer(function(req, res) {
var q = url.parse(req.url, true);
var filename = "." + q.pathname;
if(filename == "./") {
fs.readFile('index.html', function(err, data) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
res.end(); // end the response
});
}
else if(filename == "./contact-me") {
fs.readFile('contact-me.html', function(err, data) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
res.end(); // end the response
});
}
else if(filename == "./about") {
fs.readFile('about.html', function(err, data) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
res.end(); // end the response
});
}
else {
fs.readFile('404.html', function(err, data) {
res.writeHead(404, {'Content-Type': 'text/html'});
res.write(data);
res.end(); // end the response
});
}
}).listen(8080, console.log("Server running at localhost:8080.")); // the server object listens on port 8080
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment