express-slash icon indicating copy to clipboard operation
express-slash copied to clipboard

error with global routes

Open bonesoul opened this issue 8 years ago • 2 comments

using express-slash with routes like

app.get('*', function(req, res, next) {

crashes my error handlers. any ideas?

bonesoul avatar Jan 18 '17 08:01 bonesoul

here is an example code;

    app.get('*', function(req, res, next) {

        const get_ip = require('ipware')().get_ip;
        let ip_info = get_ip(req);

        if (req.user) {
            req.session.last_ip = ip_info.clientIp; // set the last ip.
            req.session.last_activity = Date.now(); // set the last activity.
        }

        next();

    });

    // ========================================
    // routes.
    // ========================================

    require('./routes/routes')(app);

    // ========================================
    // fix routes without a slash.
    // ========================================

    app.use(slash());

    // ========================================
    // error handling.
    // ========================================

    -- error handling here---

in this case express-slash will cause an inifinite redirect route on an nonexisting url; like if you query localhost/404

it'll start redirecting; localhost/404 -> localhost/404/ -> localhost/404 and so on..

basically because testStackForMatch() just matches that * route :)

bonesoul avatar Jan 18 '17 10:01 bonesoul

I have a similar problem with a similar route (/api/*). Wondering if there's a good solution for this as well.

tnguyen14 avatar Oct 12 '17 14:10 tnguyen14