stefano
stefano

Reputation: 335

Routing from http POST to http GET in Express

I have some questions about routing using Node.js and Express. I have a running application where you can add items in different ways (e.g. via 'URL'). Let´s describe that further:

I have to different routes: /create: Choose adding option in a form with dropdown list (e.g. via 'url') /create/validate: Form which shows the retrieved data

/create:

When pressing the "Add" button on my main page an GET request on this route will render a form where you can choose the inserting option. In this form you can choose in a dropdown list one option (e.g. 'URL') and press the "Submit" button. The POST request collect the entered information which are available inside the req.body.

Before sending the entered data to my database I want to perform a validation. That means, the entered data is rendered in a validation form (which is shown to the user) on route /create/validate.

The problem here is, that I can´t send the entered data from /create POST request to the /create/validate GET request for the form. Right now my /create POST request is redirecting to the /create/validate route and saving the url content in a local variable, which can be then accessed in my /create/validate GET request for further processing.

So summing up: I want the entered data of the /create POST request available in the /create/validate GET request. Is there any other way instead of saving the content to a local variable?

Upvotes: 0

Views: 37

Answers (1)

CR7
CR7

Reputation: 587

The best way is just keep a global variable somewhere outside your http handlers. That data will stick around, between request, on the server side, as long as the node process is running.

Otherwise an other solution is using localstorage for node.js https://www.npmjs.com/package/node-localstorage

Upvotes: 1

Related Questions