Router.use requires middleware function?

12,920

Solution 1

Your login_routes.js should look something like this (in the context of express):

var express = require('express');
var router = express.Router();

// GET request to /login
router.get('/', function(req, res, next) {
    // do something
});

// POST request to /login
router.post('/', function(req, res, next) {
    // do something
});

module.exports = router;

In your app.js you use

var login_routes = require('./login_routes');
...
app.use('/login', login_routes);
...

Have a look at the code generated by the express-generator module. That is a starting point for express webserver apps.

Solution 2

People have already offered hints at the solution in comments.

The first issue is that you need to export your "sub" app from login_routes.js. To do so, change this:

var app = express();

Into this:

var app = module.exports = express();

Secondly, you are—probably unintentionally—creating routes for GET /login/login and POST /login/login. To solve this, use this in login_routes.js:

app.route('/').get(...).post(...);

This is because the root path in your sub app (the one in login_routes.js) will get mapped to the path used in app.use() in your main app (server.js).

Share:
12,920
user3649245
Author by

user3649245

Updated on June 04, 2022

Comments

  • user3649245
    user3649245 almost 2 years

    So I'm trying to seperate my login routes in a seperate JS file called login_routes.js

    I keep getting this specific error:

    TypeError: Router.use() requires middleware function but got a Object at Function. (/Users/ethanthomas/Desktop/mean-stuff/express-server/node_modules/express/lib/router/index.js:446:13)

    Not entirely understanding what it's asking me to implement?

    login_routes.js:

    var express = require('express');
    var app = express();
    
    app.route('/login')
    
    .get(function(req, res, next) {
        res.send('this is the login form');
    })
    
    .post(function(req, res, next) {
        console.log('processing');
        res.send('proccessing the login form!');
    });
    

    server.js:

    var express = require('express');
    var app = express();
    var path = require('path');
    var adminRoutes = require('./app/routes/admin_routes');
    var loginRoutes = require('./app/routes/login_routes');
    
    app.use('/admin', adminRoutes);
    app.use('/login', loginRoutes);
    
    
    //send our index.html file to the user for the home page
    app.get('/', function(req, res) {
        res.sendFile(path.join(__dirname + '/index.html'));
    });
    
    //start the server
    app.listen(1337);
    console.log('leet is the magic port');
    
  • robertklep
    robertklep almost 9 years
    That doesn't do the same. For one, you're splitting the GET and POST into two separate routes, and instead of exporting two symbols it would suffice to export the app.
  • user3649245
    user3649245 almost 9 years
    I'd like to use get/post methods only on /login...any ideas?
  • Jayesh Chandrapal
    Jayesh Chandrapal almost 9 years
    @user3649245 I edited my answer for same /login route for both get and post.
  • Jayesh Chandrapal
    Jayesh Chandrapal almost 9 years
    @robertklep Please remove downvote. I have corrected it.
  • optional
    optional about 7 years
    The second statement helped me, thank you! I wasn't aware of the mapping :D