Created
August 7, 2017 07:09
-
-
Save harish2704/7e74e2268bdb9998f1e795cefe450116 to your computer and use it in GitHub Desktop.
Simple Nodejs-Express reflection API
This file contains 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
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