Created
September 5, 2019 18:15
-
-
Save koi8-r/4b7e0c497d9aff7a9679c37079483a72 to your computer and use it in GitHub Desktop.
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
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