Skip to content

Instantly share code, notes, and snippets.

@flitbit
Created April 29, 2014 00:43
Show Gist options
  • Save flitbit/11388074 to your computer and use it in GitHub Desktop.
Save flitbit/11388074 to your computer and use it in GitHub Desktop.
Dead-simple Echo Webserver - expects, returns JSON
var express = require('express'),
bodyParser = require('body-parser'),
util = require('util')
;
var app = express();
app.use(bodyParser());
app.get('/', function(req, res){
res.send('You probably want to perform an HTTP POST');
});
app.post('/', function(req, res){
util.log("body: "+util.inspect(req.body, false, 99));
res.json(req.body);
});
app.listen(3000);
@flitbit
Copy link
Author

flitbit commented Apr 29, 2014

To run this simple server you should have node.js installed.

The easiest way to run it is to put save app.js to a file in a clean directory. Open a terminal and change directories to your new clean directory and run these commands:

npm install express
npm install body-parser
npm install jade

Once you've got those dependencies in place run:

node app.js

After the server is running you can send it a post using curl, or any other mechanism that can post JSON.

curl -X POST -H "Content-Type: application/json" -d '{"nothing":"interesting","posted":"here"}' http://localhost:3000/

The sole purpose of this simple server was to sanity check my answer to this question on stack-overflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment