Create a gist now

Instantly share code, notes, and snippets.

@nbqx /app.js
Created Dec 19, 2012

What would you like to do?
var fs = require('fs');
var vm = require('vm');
var _ = require('underscore');
var src = fs.readFileSync('test.js');
var code = (_.template("'use strict';(<%= src %>)(req,res)"))({src: src});
var app = require('express')();
app.get('/',function(req,res){
var cxt = {
req: req, res: res,
console: global.console
};
var proc = vm.createScript(code);
proc.runInNewContext(cxt);
});
app.listen(3000);
function(req,res){
console.log(req);
var ret = {success: true, text: 'this is from test.js', time: new Date()};
res.setHeader('Content-Type', 'application/json');
res.send(ret);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment