Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
HTTP Server
var http = require('http');
var fs = require('fs');
var url = require('url');
var path = require('path');
var app = http.createServer(function(request, response) {
var parsedURL = url.parse(request.url, true);
console.log(parsedURL);
fs.readFile(path.join('.', parsedURL.pathname), 'utf8', function(err, contents) {
if (err) {
response.statusCode = 404;
response.end('404');
return;
} else {
response.statusCode = 200;
var regex = new RegExp(parsedURL.query.from, 'gi')
response.end(contents.replace(regex, parsedURL.query.to));
}
});
});
app.listen(8080, function() {
console.log('Server is listening on 8080');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment