Runny Yolk Runny Yolk - 1 year ago 63
Node.js Question

Node.js / Express Passing Data Between Pages Without Messing Up RESTful Routing

I have an Express app, where the landing page invites people to enter their email address and then click on "Sign Up" or "Log In", depending on whether they have an account yet or not.

The user is then taken to "/signup" or "/login", via a POST request so that the email address they entered can be passed along.

But now that I've used POST for both of these routes, I have to POST to another URL to process their sign up data or log in credentials, like app.POST("/register") and app.POST("/enter"). This feels pretty sloppy, and definitely messes with the RESTful routing that I'd like to stick to.

I know I can pass the data through the URL via a GET request, but I don't really like that solution either.

What's the best approach to take in this situation? Should I just use GET requests and stop being so fussy? Or is there another way I haven't though of yet?

Thank you kind peoples!

Answer Source

I can see why you feel this messes with the RESTful approach, and an alternative method would be to store the email address client-side and not POSTing it to the server - until they actually sign up.

You could use the LocalStorage Web API to store the email address in the client browser, and then load it when the user hits the /signup route.

let email = document.querySelector('input[name="email"]').value
localStorage.set('email', email)

then later on when the user hits the /signup route you would do:

let email = localStorage.get('email')
document.querySelector('input[name="email"]').value = email

This would have to be in a <script> tag for the two routes.

You could also use sessionStorage which gets cleared when the user closes their browser, while localStorage is a permanent storage solution.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download