Skip to content

Instantly share code, notes, and snippets.

@iainjreid
Last active August 2, 2018 19:28
Show Gist options
  • Save iainjreid/aca3c3f349b8383093e3 to your computer and use it in GitHub Desktop.
Save iainjreid/aca3c3f349b8383093e3 to your computer and use it in GitHub Desktop.
Serve static files, HTML, CSS, JS, etc, using Node.js
var fs = require('fs'),
http = require('http');
http.createServer(function (req, res) {
console.log('Incoming request');
fs.readFile('.' + req.url, function (err, file) {
if (err) {
res.writeHead(404, {
'Content-Type': 'text/plain'
});
res.end('File not found');
} else {
res.writeHead(200, {
'Content-Type': 'text/plain'
});
res.end(file);
}
});
}).listen(8080, function () {
console.log('Server listening')
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment