Middleware to handle callback hell expressjs - node.js

Yesterday I came into a situation where in one "action" I need to do 2 db lookups and an insert.
The user submits data through a post, and I need to validate the token,validate another id and then insert the data he sent me.
At first I thought about using async, but I didn't find it as elegant as I would like it to be.
So I dag deeper in the expressjs and saw you can define middleware for specific routes.
So what if I created a middleware just for this route that handles the validation of the token and that other id? and the only thing that the action does is to actually insert the data?
Is that a good solution?

Related

Which executes first? Query or Mutation in graphql?

I'm new to graphql and just wanted to ask which operation executes completely first? Query or Mutation? I went through the docs and it mentions queries are in parallel, and mutations are serial, but if both are present, which one completes first?
I'm trying to convert a GET and POST rest API route to graphql. And the request body of the post request will be the response from GET call. Can anyone tell me how can I implement this logic? I couldn't find any demo/online tutorial for same. Here's one of my previous questions regarding graphql for code purposes.

My guard automatically embed to another method in controller Nestjs

I created a bookmark method that uses a JwtAuthenticationGuard and IdentifyRecipeGuard. But when I put that method under another method that uses IdentifyRecipeGuard, for some reason IdentifyRecipeGuard gets executed when I try to make a request to the bookmark method. And when I move the bookmark method right under the findOne method it works well. Why is this happening?
When a request gets processed by your server the routes will be evaluated in the order that they are defined and the first one that matches will be executed. However, because :id comes first this controller method will be used and the request will never make it to your bookmark handler.
In your example, if someone sends a request to /bookmark then both the :id route and the bookmark route are potential matches (since the framework has no way of knowing that bookmark isn't the id of something).
To fix this simply move the more specific routes above the ones that have route parameters.
This issue is unrelated to NestJS and you would be seeing the exact same issue if you were building an app with Express directly

Implementing "side effects" in Express routes?

I'm looking for some advice on how to achieve something the "proper" way in Express.
When routes on my API are hit, I need to send a bunch of "side-effect" data to all clients via a websocket. All the websocket stuff is done and working, my question is mostly conceptual. So, for example, a POST is made to /message, after the route controller has handled the request and sent a response, I need to send some updated data regarding other data models via websocket to all clients.
I could, of course, just send the WS message from the route controller, but that feels haphazard and unstructured. I'm sure there must be a "proper" way to do it! I did wonder about creating a middleware that runs after the route controller that either examines the request and sends the appropriate updates, or takes something passed from the route controller and uses that to determine what to send. Does anyone have any suggestions?
Thanks!

Make sure nodejs express route can only be called once, until finished

My endpoint
POST example.com/user/transfer/
should not be able to be called by the same user until his earlier request has been processed. How do I do that?
Ideas
Keep a request Nonce in the user table (database) and make sure it can only be used once.
?
I might be not wrapping my head around it the right way.

GETting a document within a Document Update Handler

Is it possible to query (GET) a document from within a document update handler in CouchDB?
I have written a simple document update handler in CouchDB 2.0 to accept a POST from a third party (CognitoForms). This works fine, and I take the ID from their JSON payload and use that as the doc _id.
You can then specify an 'update' URI in CognitoForms, so I could create a new update handler or use the same one. However, in CognitoForms:
The update does a POST rather than a PUT
There does not appear to be a way to send any query parameters
As the ID for the document which needs to be updated is within the body, I could use this to query the database for the document, get the _rev, and return the payload with the _id and _rev to perform the update. However, I simply don't know if I can do such a query within the update handler. I feel like I am either missing something obvious, or there is a very good reason that I wouldn't be allowed to do that.
Thanks very much
edit: I should add that I understand I could create a small application to parse the request before forwarding on to couchdb, but I was interested to see if I could implement this in couchdb only to understand how far I can get without another layer!
In your particular case, it's quite hard to do this. A document update handler is basically a pure function that gets the data it needs and returns a response, but it has no way to reach out into the database.
If you add a doc id to the url, the update function gets the doc from the database as a parameter. For details see the CouchDB docs for update functions.
The way to a possible solution is to use a rewrite in CouchDB in order to extract the id from the body. In CouchDB 2.0, a new way for rewrites as functions has been introduced.
For pushing the limits, using a rewrite function for this sounds like fun. But for a production use case, it's probably easier and more maintainable to create a small node.js app that parses the body.

Resources