Skip to content

Instantly share code, notes, and snippets.

@koi8-r
Created September 5, 2019 18:15
Show Gist options
  • Save koi8-r/4b7e0c497d9aff7a9679c37079483a72 to your computer and use it in GitHub Desktop.
Save koi8-r/4b7e0c497d9aff7a9679c37079483a72 to your computer and use it in GitHub Desktop.
const express = require('express')
const vm = require('vm')
let app = express()
app.use(function(req, res, next) {
req.body = ''
req.setEncoding('utf8');
req.on('data', (chunk) => {
req.body += chunk
})
req.on('end', () => {
next()
})
})
app.post('/', (req, res) => {
if (req.body) {
let ctx = {}
try {
let x = vm.runInNewContext(req.body, ctx)
ctx['res'] = x || null
res.send(ctx)
} catch (ex) {
res.send('Error')
}
}
})
app.listen(3000, () => {
console.log('Listening on port 3000')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment