Skip to content

Instantly share code, notes, and snippets.

@petrovg
Last active December 19, 2015 00:29
Show Gist options
  • Select an option

  • Save petrovg/5869173 to your computer and use it in GitHub Desktop.

Select an option

Save petrovg/5869173 to your computer and use it in GitHub Desktop.
A noddy little REST test server, that allows you to set up a bunch of resources using PUT requests and then serves them back at you. This makes setting up test cases for REST consumers very simple. Run using 'node Noddy.js'. Default port is 9210 - look at the bottom to change that
var http = require('http')
var respond = function(res, s) {
res.writeHead(200);
res.write(s);
res.end();
}
var makeService = function(resources) {
var headers = {};
return http.createServer ( function(req, res) {
console.log('Requested url is: ' + req.url);
console.log('Request method is: ' + req.method);
if (req.method === 'PUT') {
headers[req.url] = req.headers; req.on('data', function(chunk) {
console.log('==> ' + chunk);
resources[req.url] = chunk;
res.end(); });
}
else if (typeof(resources[req.url]) === 'undefined') {
res.writeHead(404);
res.end();
}
else {
for (h in headers[req.url]) {
res.setHeader(h, headers[req.url][h]);
}
respond(res, resources[req.url]);
}
});
}
makeService( {"/boo" : "<html><h1>Boo</h1></html"} ).listen(9210);
console.log("Running");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment