簡體   English   中英

next()不是函數錯誤Node.js

[英]next() is not a function error Node.js

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 --------------------------^

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM