Skip to content

Instantly share code, notes, and snippets.

@mariyadiminsky
Last active July 20, 2016 06:05
Show Gist options
  • Save mariyadiminsky/95440a685479c1057f4254fac135f12e to your computer and use it in GitHub Desktop.
Save mariyadiminsky/95440a685479c1057f4254fac135f12e to your computer and use it in GitHub Desktop.
var http = require('http');
var fs = require('fs');
// STEP #2.4 Let's serve actual files! Uncomment below:
var bunnyServer = http.createServer();
bunnyServer.on('request', function(req, res) {
// If it's a GET request:
if(req.method === 'GET') {
// and if the GET request is the homepage:
if (req.url === '/') {
// read the html file asynchronously, and call callback.
fs.readFile(__dirname + '/index.html', function(err, file) {
// if there is an error with retrieving the html page
// then print the error:
if (err) console.log(err);
// otherwise let the client know the file is OK, and let the
// client know I'm sending over an html page.
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(file);
})
}
// if the GET request is for the css:
if (req.url === '/style.css') {
// read the css file asynchronously, and call callback.
fs.readFile(__dirname + '/style.css', function(err, file) {
// if there is an error with retrieving the css stylesheet
// then print the error:
if (err) console.log(err);
// otherwise let the client know I'm sending over
// a css stylesheet and the file is OK.
res.writeHead(200, {'Content-Type': 'text/css'});
res.end(file);
})
}
}
}).listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment