I am using express.js server and try to inject a middleware to the app instance.
app.configure(function() {
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express["static"](PUBLIC_PATH));
});
hello = function(req, res, next) {
console.log("hello world");
next();
};
app.configure('development', function() {
app.use(hello());
app.use(express.errorHandler({
dumpExceptions: true,
showStack: true
}));
});
I am trying to inject Hello() function in to the current app instance. But when I run the code from console, it returns the "hello world", but with some errors in the console too. Where did I do wrong.
hello world
/Users/mma/Desktop/mustdelete/aaron/server/app.coffee:13
return next();
^
TypeError: undefined is not a function
at Object.module.exports.exports.hello (/Users/mma/Desktop/mustdelete/aaron/server/app.coffee:13:14)
at Function.module.exports.app.configure.app.set.layout (/Users/mma/Desktop/mustdelete/aaron/server/app.coffee:16:30)
at Function.app.configure (/Users/mma/Desktop/mustdelete/aaron/node_modules/express/lib/application.js:395:61)
at module.exports (/Users/mma/Desktop/mustdelete/aaron/server/app.coffee:15:9)
at Object.<anonymous> (/Users/mma/Desktop/mustdelete/aaron/server/index.js:2:34)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:492:10)
You don't have to pass the return of the function (with parentheses) hello(), just the name of the function (without parentheses), app.use(hello).