Skip to content

Instantly share code, notes, and snippets.

@harish2704
Created August 7, 2017 07:09
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 harish2704/7e74e2268bdb9998f1e795cefe450116 to your computer and use it in GitHub Desktop.
Save harish2704/7e74e2268bdb9998f1e795cefe450116 to your computer and use it in GitHub Desktop.
Simple Nodejs-Express reflection API
module.exports = function( app ){
app.post('/__reflection', function( req, res, next ){
const ip = req.headers[ 'x-forwarded-for' ] || req.connection.remoteAddress;
console.log( 'IP', ip );
if( ip !== '::ffff:127.0.0.1' ){
return next();
}
let fn;
try {
eval( 'fn = ' + req.body.code );
} catch (e) {
e.status = 500;
return next(e);
}
Promise.resolve()
.then(fn)
.then( data => res.send({ data}) )
.catch(next);
});
}
/* Usage
// Execute the below function on server using this API and get results.
function toBeExecutedOnServer(){
return 'Process Id is ' + process.pid;
}
function toBeExecutedOnServer2(){
return Promise.resolve().then(function(){ return {pid: process.pid } })
}
request.post( '/__reflection', { json: true, body: toBeExecutedOnServer.toString() }, console.log )
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment