Skip to content

Instantly share code, notes, and snippets.

@jazzyjackson
Created May 30, 2017 01:28
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 jazzyjackson/e1f3ad8944ee26fefa5267af729f9c06 to your computer and use it in GitHub Desktop.
Save jazzyjackson/e1f3ad8944ee26fefa5267af729f9c06 to your computer and use it in GitHub Desktop.
const fs = require('fs')
const exec = require('child_process').exec
const http = require('http')
http.createServer((request,response) => ({
'GET': () => fs.createReadStream('.' + request.url, 'utf8')
.on('error', err => response.end(JSON.stringify(err)))
.pipe(response),
'POST': () => exec(decodeURI(request.url.split('?')[1])).stdio.forEach(stdio => stdio.pipe(response)),
'PUT': () => request.pipe(fs.createWriteStream('.' + request.url, 'utf8'))
.on('finish', () => response.end()),
'DELETE': () => fs.unlink('.' + request.url, err => response.end( err ? JSON.stringify(err) : 'OK'))
})[request.method]()).listen(3000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment