Mar*_*ian 4 javascript node.js express
var express = require('express');
var app = express();
var middleware = {
requireAuthentication: function(req, res, next){
console.log("private route hit");
next();
}
};
app.use(middleware.requireAuthentication());
app.get('/about',
function(req, res){
res.send('You clicked on about!');
}
);
var projectDir = __dirname + '/public';
app.use(express.static(projectDir));
app.listen(3000), function(){
console.log('Static service started');
};
Run Code Online (Sandbox Code Playgroud)
我得到的错误(当试图运行服务器时)next()不是一个函数.我一直在关注Nodejs的教程,它对他们来说效果很好.我在这有什么问题?
T.J*_*der 10
这一行:
app.use(middleware.requireAuthentication());
Run Code Online (Sandbox Code Playgroud)
调用您的方法并将其返回值传递给app.use.你不是用任何参数调用它,所以next参数自然是undefined.
摆脱()所以你将函数而不是其结果传递给app.use:
app.use(middleware.requireAuthentication);
// No () here --------------------------^
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4531 次 |
| 最近记录: |