11
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
  • foo(bar()) always calls bar first and passes the return value to foo. requireAuthentication is not supposed to be called by you. Commented Dec 22, 2016 at 17:08

1 Answer 1

21

This 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 --------------------------^
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.