Last active
August 2, 2018 19:28
-
-
Save iainjreid/aca3c3f349b8383093e3 to your computer and use it in GitHub Desktop.
Serve static files, HTML, CSS, JS, etc, using Node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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