Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.