A Simple HTTP POST Server in Node.js

2012-10-16 21:48:16 by jdixon

In the process of hacking on a plugin for the Uptime project I realized that I needed a simple HTTP server capable of receiving and dumping JSON data via POST. I'm well aware of the awesome Python SimpleHTTPServer module, but alas it doesn't support POST requests.

Fortunately I was able to throw together a quick little server using the sample HTTP server on the Node.js project website along with their API docs. I know this is a ridiculously simple daemon, but someone else might find it as useful as I did.

var http = require('http');

http.createServer(function (req, res) {
  var body = '';
  req.on('data', function(data) {
    body += data;
  });
  req.on('end', function() {
    console.log(JSON.parse(body));
  });
  res.writeHead(204, {'Content-Type': 'text/plain'});
  res.end();
}).listen(5001, '127.0.0.1');

console.log('Server running at http://127.0.0.1:5001/');

Add a comment:

  name

  email

  url

max length 4000 chars