Skip to content

Instantly share code, notes, and snippets.

@whizzter
Last active June 6, 2017 07:38
Show Gist options
  • Save whizzter/fe37aa03bb2e9579e304c16cc8e12db2 to your computer and use it in GitHub Desktop.
Save whizzter/fe37aa03bb2e9579e304c16cc8e12db2 to your computer and use it in GitHub Desktop.
automatically exporting a module with koa (possibly insecure)
exports.bah=function(count,name){
let out="";
while(count--)
out+=name+" ";
return out;
};
// Install requirements with: npm install --save co co-body koa-better-router koa@2
const app=new (require("koa"))();
const router=require("koa-better-router")().loadMethods();
const co_body=require("co-body");
const mymod=require("./mymod");
// Test the below with: curl -d "[3,\"Hello\"]" http://localhost:8080/mymod/bah
router.post("/mymod/:myfun",function*() {
const fun=this.route.params.myfun;
const args=yield co_body.json(this,{limit:"10kb"});
const mod=mymod;
// Only allow invoking direct properties on the object (ie restrict the prototype chain methods from being invoked)
if (Object.prototype.hasOwnProperty.call(mod,fun)) {
this.body=mod[fun].apply(null,args);
} else {
this.body="No such method "+fun;
}
});
app.use(router.middleware());
app.listen(8080);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment