Skip to content

Instantly share code, notes, and snippets.

@alanshaw
Last active October 27, 2016 12:06
Show Gist options
  • Save alanshaw/133562ace098870f2d178295b0ea7068 to your computer and use it in GitHub Desktop.
Save alanshaw/133562ace098870f2d178295b0ea7068 to your computer and use it in GitHub Desktop.
Mock JSON server
const Http = require('http')
function createMockJsonServer ({ statusCode = 200, body, headers = {} }) {
return Http.createServer((req, res) => {
res.statusCode = statusCode
res.setHeader('Content-Type', 'application/json')
Object.keys(headers).forEach((k) => res.setHeader(k, headers[k]))
if (body) res.write(JSON.stringify(body))
res.end()
})
}
module.exports = createMockJsonServer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment