Make sure nodejs express route can only be called once, until finished - node.js

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.

Related

How to create a cookie when making an axios request?

I'm building an app for personal use that gets data from Walmart and then displays clean data depending on what I want. I am having a problem. Every day, when I run my app. I have to insert a new cookie inside the headers because I guess it changes every day? Not sure but I was wondering if there is a way to create a cookie automatically or something like that.
I am using axios to make my get request. I already tried using npm cookie-parser, withCredentials: true, and a bunch of other things and none of them work. When I try it, the get request just fails. The only way it works is when I manually send a request, steal that cookie and put it inside my app.
Thank you.

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!

QR code best approach for POST request from REST API

I'm setting up a website that will be mobile focused and one of the features I wan't to implement is users to be able to log an entry by just scanning a QR code.
For what I read is not really possible to make a POST request directly from a QR code, so I was thinking in two different options:
1. Make a GET request and then redirect that inside my server to a POST route in my routes.
So the URL would be something like https://example.com/user/resources/someresourceid123/logs/new and then this would create a POST request to https://example.com/user/resources/someresourceid123/logs/ and create the new entry to then send a response to the user but I'm not really sure this is the best approach or if it's possible at all.
My POST request only requires the resourceid which I should be able to get from req.params and the userid which I get from my req.user.
2. Do my logic and log the entry to my DB using the GET request to https://example.com/user/resources/someresourceid123/logs/new.
This would mean that my controller for that request will do everything needed from the GET request without having to make an additional POST request afterwards. I should be able to get both the resourceid and userid from the req object but not sure if being a GET request limits what I can do with it.
If any of those are possible, which would be the best approach?
I'd propose to go with a second option simply for the sake of performance. But you need to make sure your requests are not cached by any proxy, which is usually the case with GET requests.

Persisting and identifying data in nodejs without a database

I am new to node and back end so please excuse me if my question seems dumb.
My use scenario is as follows:
I have a simple UI that will make only one ajax call at a given time. I have a node js backend that will take this call, make a login call to a webservice and use the response data to make another get call. When the call is over I will make a logout call and delete the login response data.
Problem:
The problem is that being single threaded and async and having no database the logout call will invalidated the login data for all the calls coming afterwards or that are in progress. I need a way to persist and encapsulate the data for each call without blocking the IO for each request.
Solution:
The only thing that I thought so far was to save the login data into dynamic created variables (based on the UI caller ID) and delete those vars when the call is over. However this seems like a very error prone solution that might also cause memory leaks.
I do not want to persist the data into a database and could not figure other solution, can you please advise?

Middleware to handle callback hell expressjs

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?

Resources