-
-
Notifications
You must be signed in to change notification settings - Fork 103
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
Determine if route exists #95
Comments
Hi @shennan , Your first example is almost there: getting to that last callback function with no error is when there is no route, so you should just change your test to As far as the second example, that is not possible, as any middleware can rewrite the url like |
Thanks for the response @dougwilson. The problem with the above is that the 'finish' callback is executed always, whether the route exists or not. But by the sound of it (due to possible rewrites of the url) there is no clear way of determining if an exact route exists during runtime? On a slightly different note... Is there any way of getting the response object in the 'finish' callback? Reason being that I want to somehow promisify the execution of a route. Something like:
I wrote something like this:
But the problem with that is that it adds a new route each time it's called. It works perfectly; but only once... because I don't call Ideally I want the ability to add some middleware that is only added for that single execution of the route. Such that I can capture results but not effect the make-up of the stack. Am I trying to square-peg a round hole? |
Hi @shennan the callback should not be called if you have a route there. Can you share your route code? For example, const route = Router().use('/some/route', (res, req, next) => next()) Is considered a route that does not exist in this module, as it calls
It is not provided, as the response object is not something produced by the As far as your later part of the quest, I think I lost you there, and I'm not sure what exactly you are attempting to do, not sure what an answer is. |
Ok, so the library assumes that a route is only fulfilled when
Which allows me to modularise my middleware and separate concerns. But it sounds like, within this setup, I should not call The latter part of the question is really to do with how my application consumes and 'runs' the route. Because I'm not using a server, I've just been handing empty objects as the response. But I suppose in order to get a response from the executed route, I should send a response object akin to Thanks again for the continued advice. 👍 |
Middlewares are not the same as the route handlers. A middleware is typically expected to call I guess, what are you trying to do with the module? I thought perhaps you were just trying to write some unit tests around it, but still would be using it to write something that would actually work under the HTTP server, is that not the case? |
Thanks @dougwilson Perhaps my use case is a strange one. Essentially I wanted to use router without a server such that I could run it in various browser and node distros. We have a core product which needs to have several interfaces. One is a server and more akin to a traditional express application, the other is a devtools extension, an electron interface etc. And there may be others. So I'm using it as a sort of headless router, which will contain all of our business logic. Best way to describe it is:
Basically we wanted to leverage the modulability of routes and middleware, and have our main business logic operate outside of a particular interface. Obviously it's more complex than the above but it should go some way at explaining the use case. |
Is there a way of determining if an exact route exists and running some code if it doesn't. For example:
Or perhaps something like:
I'm currently using the library without any HTTP server, just as a router and middleware engine. So
req.send()
doesn't exist for me (and shouldn't), I simply callnext()
until there is no more middleware in the stack.Any direction on this would be great.
The text was updated successfully, but these errors were encountered: