-
Notifications
You must be signed in to change notification settings - Fork 84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Removing a route #52
Comments
Hi! Unfortunately this has been discussed many times and that feature is outside the scope of this project. The router is optimized in such a way that removing routes would u do those optimizations. You'll want to use a different module that better fits your use-case :) |
OK, thanks for your input @dougwilson could you suggest a module to use instead for this use case? It seems using express directly would have the same issue. |
I have not created one, or ever had the need for one, so I don't know off-hand. You're certainly welcome to create one and publish to npm! Express uses this as the default router, but you can plug in any router you desire, so it doesn't restrict Express to any one particular router implementation. |
OK, creating a whole different module when I've already based my application around this which does 90% of what I need seems like the wrong way to go. Would the right way to go be to reconfigure the route to explicitly return a 404? would that still affect performance? const Router = require('router');
const router = Router()
router.get('/', function (req, res) {
res.setHeader('Content-Type', 'text/plain; charset=utf-8')
res.end('Hello World!')
});
var server = http.createServer(function(req, res) {
router(req, res, finalhandler(req, res))
})
//some event happens instructing to remove the route
setTimeout(() => {
router.get('/', function (req, res) {
res.writeHead(404)
res.end('Hello World!')
});
}, 1000);
server.listen(3000) |
Hi @willm since this is an append-only router, if you added that second 404 route, it would be at the end, and so the previous route you wanted to remove would still exist, unaffected. It really depends on exactly what you are trying to achieve, but, say you were building something that dynamically built a router from some configuration. You could remove routes by simply building a new router and using a middleware to swap it, for example: // in the main app
app.use(createDynamicRouter(config))
// somewhere else
function createDynamicRouter(config) {
var router = buildRouterFromConfig(config)
config.on('change', function () {
router = buildRouterFromConfig(config)
})
return function (req, res, next) {
return router(req, res, next)
}
} |
Hi , I'm writing an application that requires re configuring of routes while the app is running. Therefore I need to be able to remove routes that were added previously. I was thinking about doing this by removing the route from
router.stack
by filtering for the route with the required path. I was wondering if it would be a useful thing to add to this module?so something like:
The text was updated successfully, but these errors were encountered: