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 routes at runtime #2596

Closed
akyoto opened this issue Mar 16, 2015 · 3 comments
Closed

Removing routes at runtime #2596

akyoto opened this issue Mar 16, 2015 · 3 comments
Assignees

Comments

@akyoto
Copy link

akyoto commented Mar 16, 2015

First off, let me say that I'm an Express beginner and so far I'm very impressed with what it can do.
I set up my node.js application to watch file changes and those file changes actually can incur the need to alter routes. Therefore I searched the routing documentation and it seems there is no official, documented way to do it.

I quickly searched if this issue already existed on GitHub but I couldn't find it via "remove" or "remove routes".

A few minutes of Google-fu later it seems there are some ways to do it:
http://stackoverflow.com/questions/15027514/node-js-remove-route-while-server-is-running
http://stackoverflow.com/questions/10378690/remove-route-mappings-in-nodejs-express

However those don't seem to be well documented and I don't see why Express shouldn't have an officially documented function à la "app.remove()" or a similar mechanism to remove routes.

If there is something I missed please let me know.

@dougwilson
Copy link
Contributor

Correct, there is no official way because we cannot optimize things if the stack can be mutable at runtime. The way we recommend you do it is to simply swap a router at runtime:

var express = require('express')

var app = express()
var router = undefined

// this should be the only thing on your app
app.use(function (req, res, next) {
  // this needs to be a function to hook on whatever the current router is
  router(req, res, next)
})

function defineStuff() {
  router = express.Router()

  // define everything on _router_, not _app_
  router.get('/', ...)
}

// now do you watch and when something changes
// call defineStuff()

@akyoto
Copy link
Author

akyoto commented Mar 16, 2015

Wow, now that is what I call quick support!

I see, so performance is the reason for the lack of this method.
Thank you, I will try the method you suggested.

@dougwilson
Copy link
Contributor

Correct :) Because Node.js is async, when you start to alter the routes, there are likely various requests in various states within the stack. Requests may re-enter the stack, and since this is done by index, removing a route will cause strange behavior for in-flight requests. It's the same reason we limit to only adding new routes at the end of the stack :)

I'm here if you still have questions, so feel free to reply back here if you need :) As for quick, it's really timing as I just happen to be on mucking with all the Express repos right now :D

@expressjs expressjs locked and limited conversation to collaborators Oct 28, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants