Skip to content
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

Closed
willm opened this issue Dec 7, 2016 · 5 comments
Closed

Removing a route #52

willm opened this issue Dec 7, 2016 · 5 comments
Assignees
Labels

Comments

@willm
Copy link

willm commented Dec 7, 2016

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:

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.remove('/');
}, 1000);
server.listen(3000)
@dougwilson
Copy link
Contributor

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 :)

@willm
Copy link
Author

willm commented Dec 7, 2016

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.

@dougwilson dougwilson self-assigned this Dec 7, 2016
@dougwilson
Copy link
Contributor

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.

@willm
Copy link
Author

willm commented Dec 8, 2016

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)

@dougwilson
Copy link
Contributor

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)
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants