Skip to content

Instantly share code, notes, and snippets.

@nilcolor
Created February 5, 2011 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nilcolor/812487 to your computer and use it in GitHub Desktop.
Save nilcolor/812487 to your computer and use it in GitHub Desktop.
Node.js pristine server template
var http = require('http'),
url = require('url'),
qs = require('querystring');
var paths = {
'__default__': function (req, res) {
console.log('noop');
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end();
},
'/-': function (req, res) {
req.addListener('data', function (chunk) { req.body += chunk; });
req.addListener('end', function () {
console.log('here is our full body: ' + req.body);
req.url.query = (req.method === 'GET') ? req.body : '';
req.body = qs.parse(req.body);
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Thanks!\n');
});
},
};
http.createServer(function (req, res) {
//URI format - scheme://domain:port/path?query_string#fragment_id
req.setEncoding('utf8');
req.body = '';
req.url = url.parse(qs.unescape(req.url));
var f = paths[req.url.pathname] || paths['__default__'];
f.call(this, req, res);
});
if (!module.parent) {
app.listen(4000);
console.log("Node server listening on port %d", app.address().port);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment