Skip to content

Instantly share code, notes, and snippets.

@eiyaya
Created May 14, 2017 10:53
Show Gist options
  • Save eiyaya/39e97286ff21ddaa467c96cf7b51412e to your computer and use it in GitHub Desktop.
Save eiyaya/39e97286ff21ddaa467c96cf7b51412e to your computer and use it in GitHub Desktop.
node run code from string
'use strict';
var vm = require('vm')
exports.getData = function* (id) {
this.query.id;
global.self = this;
var result = []
let raw = `foo = function*(){ \
let apiResult = yield self.app.urllib.requestThunk(self.app.config.dataServiceUrl + '/data/zhima/1'); \
return JSON.parse(apiResult.data); \
}`
//const code = babel.transform(raw, { "ast": false, "presets": ["stage-0"] }).code;
try {
const script = new vm.Script(raw, {
timeout: 1000
});
let foo = script.runInThisContext();
result = yield foo()
} catch (err) {
console.log(err)
}
this.body = result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment