express-slash
express-slash copied to clipboard
error with global routes
using express-slash with routes like
app.get('*', function(req, res, next) {
crashes my error handlers. any ideas?
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 :)
I have a similar problem with a similar route (/api/*). Wondering if there's a good solution for this as well.