Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why "TypeError: express.Router is not a function"

Trying to make a login app with nodejs express. However after I run it. it shows TypeError: express.Router is not a function. My express is the latest version4.13.4. Can anyone help me?Here is the code.

var User = require('../modules/user');
var config = require('../../config');

var secretKey = config.secretKey;

module.exports = function(app,express){
    var api = express.Router;
    api.post('/signup', function(req,res){
        var user = new User({
            name: req.body.name,
            username: req.body.username,
            password: req.body.password
        });

        user.save(function(err){
            if(err){
                res.send(err);
                return;
            }

            res.json({ message: 'user has been created'});
        })
    });

    return api;
};
like image 232
Haotian Wu Avatar asked Apr 07 '26 20:04

Haotian Wu


2 Answers

It should be var api = express.Router();

like image 183
Shanaka Avatar answered Apr 10 '26 18:04

Shanaka


I can't see the require to include the express library.

var express = require('express');
var app = express();

Also remember to make

$ npm install express --save

To install it.

Here is the reference

http://expressjs.com/en/4x/api.html

http://expressjs.com/en/starter/installing.html

like image 38
SD Dani Avatar answered Apr 10 '26 17:04

SD Dani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!