I have a node/express server
//I'm using these
var express = require('express');
var router = express.Router();
I'm trying to do this:
applyRouter = function(rget){
rget('/something', function(req,res,next){
console.log("anything");
});
}
applyRouter(router.get);
And I get this error
path/to/express/lib/router/index.js:509
var route = this.route(path)
^
TypeError: Cannot read property 'route' of undefined
Well OK, here's line 509
// create Router#VERB functions
methods.concat('all').forEach(function(method){
proto[method] = function(path){
var route = this.route(path)
route[method].apply(route, slice.call(arguments, 1));
return this;
};
});
What does this
refer to here?
I tried
var outerThis = this;
applyRouter = function(rget){
console.log("inside", this);
rget.apply(outerThis, ['/something', function(req,res,next){
console.log("anything");
}]);
}
applyRouter(router.get);
and
console.log("outside", this); //gives {}
applyRouter = function(rget){
console.log("inside", this);
rget.apply({}, ['/something', function(req,res,next){
console.log("anything");
}]);
}
applyRouter(router.get);
Same error... what's going on? Why can't I do this?
I can do
applyRouter = function(router){
router.get('/something', function(req,res,next){
console.log("anything");
});
}
applyRouter(router);
So I'm setting this
wrongly. What does router
expect this
to be? Where specifically does this
get changed?