Skip to content

Instantly share code, notes, and snippets.

@manchicken
Last active November 7, 2016 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manchicken/9799567bf7cadcebedd057f9e8932866 to your computer and use it in GitHub Desktop.
Save manchicken/9799567bf7cadcebedd057f9e8932866 to your computer and use it in GitHub Desktop.
This is a simple HTTP server which gives you predictable HTTP responses based on the URL. It's super simple. This requires `lodash`.
'use strict'
const http = require('http')
const _ = require('lodash')
// Create an HTTP tunneling proxy
var svr = http.createServer( (req, res) => {
// Just some simple handler routines
const handlers = _.merge(
{ '/success': { status: 200, message: 'OK' }, },
...[500, 404, 422, 409, 504, 503, 502, 403].map(
x => ({[`/${x}`]: { status: x, message: `Ack: ${x}` }})
),
{ '/timeout': { status: null, message: null } }
)
console.info(req.url)
// Use a handler based on the URL, or default to success
let handler = handlers[ req.url || '/success' ] || handlers['/success']
if ( ! handler.status ) {
console.info('Timing out on purpose')
req.socket.setTimeout(500)
return
}
res.writeHead(handler.status, {'Content-Type': 'text/plain'});
return res.end(handler.message)
});
svr.listen(6006, 'localhost')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment