Sunday 28 May 2017

Why is the ExpressJs Router only using the first route in the file for all urls?

I am new to node.js, and I am trying to create a router. I have finally gotten it to work, but it is only using the first route I list under module.exports when directing a url to a controller, unless the first route is '/about', in which case it is ignored, but, if I put '/errors' or '/' first, the pages they render (or, in the case of '/errors', the res.send() they invoke), it will be shown on every url. Please explain what is wrong with my code.

/server/routes.js:

   var express = require('express');
    router = express.Router(),
    users = require('../controllers/users'),
    errors = require('../controllers/errors');
    home = require('../controllers/home')
module.exports = function(app) {
    router.get('/about', home.about);
    router.get('/', users.new);
    router.get('*', errors.four);
    app.use('/*', router);
};



via Isaac Krementsov

No comments:

Post a Comment