Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove route mappings in NodeJS Express

Tags:

I have a route mapped as:

app.get('/health/*', function(req, res){
    res.send('1');
});

How can I remove / remap this route to an empty handler at runtime?

like image 624
lostinplace Avatar asked Apr 30 '12 04:04

lostinplace


People also ask

What is Express Router () in node js?

js server. The express. Router() function is used to create a new router object. This function is used when you want to create a new router object in your program to handle requests. Multiple requests can be easily differentiated with the help of the Router() function in Express.

Why do we use routes in Express?

A route is a section of Express code that associates an HTTP verb ( GET , POST , PUT , DELETE , etc.), a URL path/pattern, and a function that is called to handle that pattern. There are several ways to create routes.

How do I pass a file path in node js?

js: var fs = require('fs') var newPath = "E:\\Thevan"; var oldPath = "E:\\Thevan\\Docs\\something. mp4"; exports. uploadFile = function (req, res) { fs. readFile(oldPath, function(err, data) { fs.

What is a route JavaScript?

A Javascript router is a key component in most frontend frameworks. It is the piece of software in charge to organize the states of the application, switching between different views.


2 Answers

This removes app.use middlewares and/or app.VERB (get/post) routes. Tested on [email protected]

var routes = app._router.stack;
routes.forEach(removeMiddlewares);
function removeMiddlewares(route, i, routes) {
    switch (route.handle.name) {
        case 'yourMiddlewareFunctionName':
        case 'yourRouteFunctionName':
            routes.splice(i, 1);
    }
    if (route.route)
        route.route.stack.forEach(removeMiddlewares);
}

Note that it requires that the middleware/route functions have names:

app.use(function yourMiddlewareFunctionName(req, res, next) {
    ...          ^ named function
});

It won't work if the function is anonymous:

app.get('/path', function(req, res, next) {
    ...          ^ anonymous function, won't work                    
});
like image 161
laggingreflex Avatar answered Sep 19 '22 12:09

laggingreflex


Express (at least as of 3.0.5) keeps all of its routes in app.routes. From the documentation:

The app.routes object houses all of the routes defined mapped by the associated HTTP verb. This object may be used for introspection capabilities, for example Express uses this internally not only for routing but to provide default OPTIONS behaviour unless app.options() is used. Your application or framework may also remove routes by simply by removing them from this object.

Your app.routes should look similar to this:

{ get: 
   [ { path: '/health/*',
       method: 'get',
       callbacks: [Object],
       keys: []}]
}

So, you should be able to loop through app.routes.get until you find what you are looking for, and then delete it.

like image 34
Brad Avatar answered Sep 21 '22 12:09

Brad