Node.js and express : Routes

2019-03-02 13:59发布

问题:

I have some trouble using the router from Express. I want to set up my routes with several files. I get my routes folder with 2 files: routes.js and inscription.js

I do the following

var inscription = require('./routes/inscription.js');
var routes = require('./routes/routes.js');

Then

app.use('/', routes);
app.use('/inscription', inscription);

But only the routes from routes.js work...

This is the content of routes.js

var router = require('express').Router();
var bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({ extended: false});

//Homepage
router.get('/', function(req, res){
    res.setHeader('Content-Type', 'text/html');
    res.status(200);
    res.render('home.ejs');
});

//Connexion
router.post('/connexion', urlencodedParser, function(req, res){
    //Some content
});
module.exports = router;

And this is the content of inscription.js

var router = require('express').Router();
var hash = require('password-hash');
var db = require('../models/users.js');
var bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({ extended: false});

router.get('/inscription', function(req, res){
    res.setHeader('Content-Type', 'text/html');
    res.status(200);
    res.render('inscription.ejs');
});
router.post('/adduser', urlencodedParser, function(req, res){
    var passwordHashed = hash.generate(req.body.inputPassword);
    var newUser = {
        nom      : req.body.inputName,
        email     : req.body.inputEmail,
        password : passwordHashed
    };
    db.addUser(newUser);
    res.redirect('/');
});
router.post('/checkname', urlencodedParser, function(req, res){
    var user = {
        nom      : req.body.inputName
    };
    db.checkName(user, function(length){
        res.send(length);
    });
});
router.post('/checkemail', urlencodedParser, function(req, res){
    var user = {
        email      : req.body.inputEmail
    };
    db.checkEmail(user, function(length){
        res.send(length);
    });
});
module.exports = router;

The content of inscription.js works when it is pasted in the routes.js file ... So I guess it is how I import the file that is not working.

Any idea?

回答1:

This route router.get('/inscription', ...) in your inscription router is configured for the route /inscription/inscription which is likely not what you intended. This is because you've specified it in two places:

app.use('/inscription', inscription);
router.get('/inscription', ...)

So, the whole router is on /inscription from the app.use('/inscription', inscription). That means that any route the router itself defines will be added to that path.

It isn't exactly clear from your question exactly what you intend for the URLs to be. But, if you just want the above router.get() to work for a /inscription URL, then change:

router.get('/inscription', ...)

to:

router.get('/', ...)

When you use app.use('/inscription', inscription);, every single route in that router will be prefixed with /inscription. So, this route:

router.post('/adduser', ...)

will be mounted at:

/inscription/adduser

Or, if you want all the inscription routes to be at the top level too, then change:

app.use('/inscription', inscription);

to this:

app.use('/', inscription);

So that nothing is added to the path beyond what the router itself defines.