Last active
December 19, 2015 00:29
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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