gpt4 book ai didi

javascript - next() is not a function 错误Node.js

转载 作者:搜寻专家 更新时间:2023-10-31 23:30:30 24 4
gpt4 key购买 nike

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

};

我收到错误消息(尝试运行服务器时)next() 不是一个函数。我一直在关注有关 Nodejs 的教程,它对他们来说工作得很好。我在这里遇到的问题是什么?

最佳答案

这一行:

app.use(middleware.requireAuthentication());

调用您的方法并将其返回值传递给app.use。您没有使用任何参数调用它,因此 next 参数自然是 undefined。

去掉 (),这样您就可以将函数而不是结果传递给 app.use:

app.use(middleware.requireAuthentication);
// No () here --------------------------^

关于javascript - next() is not a function 错误Node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41288321/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com