Express sometimes picking wrong route - javascript

I am having a NodeJS based REST service exposed using Express (4.0.0) where I have two different routes like this:
router.get('/buckets/:bucketId/entities/bulk', getEntitiesInBulk);
router.get('/buckets/:bucketId/entities/:key', getEntityByKey);
When I send a request like this:
http://<server:port>/buckets/responses/entities/k3
The request is being handled by getEntityByKey() which I have defined there, but strangely when I bombard it with many requests it sometimes get handled by getEntitiesInBulk() and gets some error in response which is only thrown by getEntitiesInBulk().
I am totally confused about how is this possible.

Express is confused because your routes are not unique. "bulk" will sometimes be used as a :key in the first route. Simply change the signature a bit, like
router.get('/buckets/:bucketId/entities/bulk', getEntitiesInBulk);
router.get('/buckets/:bucketId/entity/:key', getEntityByKey);

Related

Axios GET request returning ECONNRESET for some URLs

I am working on an application that uses an express server to reach out to an API to fetch data. In our organisation outbound traffic requires a proxy which I have supplier to axios like below (not the real one):
let response = await axios.get(endpointUrl, {
proxy: {
host: "123.45.678.90",
port: 0000,
},
})
Passing various URLs into the axios get function returns varied results, with the following URLs returning a result:
https://www.boredapi.com/api/activity
https://api.ipify.org?format=json
https://jsonplaceholder.typicode.com/todos/1
Whereas the following URLs are returning an ECONNRESET error almost instantly:
https://api.publicapis.org/entries
https://randomuser.me/api/
https://reqres.in/api/users
I can't see any pattern between the URLs that are/are not working so wondered if a fresh set of eyes could spot the trait in them? It's important to note that all these URLs return successfully in the browser, just through this axios call being the problem.
To add to the mystery, the URLs that do work work on my machine, do work on a machine outside our organisation - so potentially a clue there?
Any help/guidance of course would be appreciated, thank you.
This error simply means that the other party closed the connection in a way that was probably not normal (or perhaps in a hurry).
For example, a socket connection may be closed by the other party abruptly for various reasons or you may have lost your wifi signal while running your application. You will then see this error/exception on your end.
What could also be the case: at random times, the other side is overloaded and simply kills the connection as a result. If that's the case, depends on what you're connecting to exactly…
Solution - This is happening because you are not listening to/handling the 'error' event. To fix this, you need to implement a listener that can handle such errors.
If the URL that work on your machine also work outside your organization and the other don't, it is most likely a problem with your proxy.
Some proxies might have configurations that makes them remove headers or change the request in a way that the target does not receive it as intended.
I also encountered a problem with axios and proxies once. I had to switch libs to make it work. To be sure, I would recommand using a lib like "request" (deprecated) juste to make sure it is not a problem with axios. There are multiple open issues on the axios repository for proxy issues.
ECONNRESET is likely occurring either because the proxy runs into some sort of error and drops the connection or the target host finds something wrong with the incoming connection and decides to immediately drop it.
That target host may either be finding a problem because of the proxy or it may be expecting something in the request that it finds is missing.
Since you have evidence that all the requests work fine when running from a different location (not through your proxy) and I can confirm that your code works fine from my location (also not running through your proxy), it definitely seems like the evidence points at your proxy as causing some problem in some requests.
One way to debug proxy issues like this is to run a request through the proxy that ends up going to some server you can debug on and see exactly what the proxy has done to the incoming request, compared to a request to that same host that doesn't go through the proxy. That will hopefully highlight some difference that you can then test to see if that's causing the problem and then eventually work on the configuration of the proxy to correct.

Express .use() callback gets called multiple times when using express.static()

I'm trying to authenticate user and once authenticated I want to create a new user in my own database with the data provided by authentication server.
The problem I am facing is that this createNewAccount() gets called 3 times. I don't exactly know why but once I removed the express.static('app/web') call this no longer happens. Still I need that call to serve static content, can someone tell me what I am doing wrong?
expressApp.use('/console',requiresAuth(),express.static('app/web'), (req,res)=>{
createNewAccount(req.oidc.user.email);
});
It might be additional requests to static files like favicon.ico, manifest.json etc.
so callback fires on each of them :)

app.use(express.static()) at the end of the program

As far as I understand when I use app.use(middleware) this middleware will be called on every request on a server.
So when I use express.static() as a middleware it will to be called on every request too but in this case it's not needed to be called on any request so why do I need to put it at the top of my program? I haven't seen anybody puts it before app.listen(). Why is this so?
Does it cause any performance degradation?
You need to have in mind the middleware works as a pipe, so, all middleware will be executed in the same order you add them.
E.G.
If you have installed the express JSON middleware, the static middleware and an authorization middleware (in this order), and your endpoint the flow of a request will be:
/ Server \
Client -----> | json -> static -> authorization -> endpoint -> EGEH |
\ /
Note: EGEH stands for Express Global Error Handler.
But if some of those elements send a response or throw an error (caught by the EGEH) the flow must be different.
In general, if you know your statics would be the last option, you could use it at the end (after the server.listen or whatever), and that would not affect the behavior, however, if that is the case may be a better option is to use the server engine (Nginx, Apache, etc) to dispatch those files to the customer instead of make node spend CPU to do that.
Middleware functions are functions that have access to the request object (req), the response object (res), and the next middleware function in the application’s request-response cycle. The next middleware function is commonly denoted by a variable named next
So that's why express static usually go first

why can't I pull HTTP requests after configured controller in nest.js

So, the person who left the company used Nest.js write this server side.
He has auth.controller, auth.service,auth.module,auth-token,jwt.strategy and jwt-payload all set up well, and I checked his module, everything is imported and the providers are being set up well.
But in front-end I do send HTTP request in any end point, I just always get code 404.
even if i wrote a simple end point like this :
*#Get('/meow')
toMeow(){
return 'meow';
}*
in his controller file, I still can't pull any HTTP request.
In his other folders controller ,I do can pull the HTTP request well with same address plus the routes
What's the reason for this?
My coworker solved it, I didnt register jwt.strategy to the Module

How to bypass an express middleware?

I'm working with an express application. There are some express routes, as
server.get('*' , ... )
etc. which perform some common operations: authentication, validation... etc.
they also decorates the response with meaningful information: i.e. in every request to the server it gives not only the expected json/html, but also information regarding the user, some app metadata that the front-end consumes etc. etc.
Let's say all this extra metadata cames in a field called extradata in every request to the server.
Now, there is a bug that is causing a problem: instead of returning its expected response (a json with a bunch of system logs), is sending only this extradata field.
I'm pretty confident the problem is in one of the middlewares, because that code that sends the response in this case is really simple, it's just a res.send() of a json. So I believe this part of the app is requiring some module that sets a middleware which causes the error. There are a lot of global vars and implicit parameters in the app so is really difficult to debug it manualluy.
I attempted to bypass such middlewares programmatically, like:
delete server._router.stack[2];
but is causing an TypeError: Cannot read property 'route' of undefined and thus preventing my app to build: sure this is not the way to go.
so, is there a way to programmatically ignore or bypass express routes that are yet set?
Even better, is there a way to programmatically tap into express middlewares and log every request and response?
(afaik, there are libreries like morgan that logs every request, but I don't think they apply to this case since I need to discriminate between middlewares).
What I generally do is simply use the next method. You can access it by simply passing it to the callback function. Something like:
app.use(function(req, res, next) {
if(...) {
next();
} else {
...
}
}
What this is going to do is go to the next middleware.
So if I understand correctly, you can check what you exactly need in the if-statement and do things accordingly.
What I would suggest is you read the Express API documentation, especially the section about middleware, which you can find here. Moreover, try to isolate the suspects and solve the issue by removing the problem, rather than deleting handlers and trying to solve the problem the easy way.

Categories