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');
};
I get the error (when trying to run the server) that next()
is not a function. I've been following a tutorial on Nodejs and it works just fine for them. What is the issue I am having here?
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');
};
I get the error (when trying to run the server) that next()
is not a function. I've been following a tutorial on Nodejs and it works just fine for them. What is the issue I am having here?
1 Answer
Reset to default 21This line:
app.use(middleware.requireAuthentication());
calls your method and passes its return value into app.use
. You're not calling it with any arguments, so naturally the next
parameter is undefined.
Get rid of the ()
so you're passing the function, not its result, into app.use
:
app.use(middleware.requireAuthentication);
// No () here --------------------------^
foo(bar())
always callsbar
first and passes the return value tofoo
.requireAuthentication
is not supposed to be called by you. – Felix Kling Commented Dec 22, 2016 at 17:08