0

我正在使用 express.js 服务器并尝试将中间件注入应用程序实例。

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
 }));
});

我正在尝试将Hello()函数注入到当前app实例中。但是当我从控制台运行代码时,它会返回“hello world”,但控制台中也会出现一些错误。我哪里做错了。

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)
4

1 回答 1

3

您不必传递函数的返回值(带括号)hello(),只需传递函数的名称(不带括号)app.use(hello)

于 2013-01-08T16:54:52.527 回答