1

I'm using Nodejs, Express and MongoDB for creating a web application. For the user registration I assign an ID to each user using a function called by the action (POST) of a form and I assign also a confirmation code (like "123456") for checking the identity of the user into another function called by the confirmation page that appear after the registration page. How can I get the user ID into the other function? (called after pressing submit into another form)

Do I have to use sessions? I think I don't know the right approach to these things

edit: I store the user data into a mongoDB database

11
  • If you have two different requests, and you'd like to persist data between them, you have to use a DB, and you're already using a DB, so just store, and then get, should be straight forward ? Commented Feb 25, 2014 at 16:38
  • yes, the problem is I have to remember the ID of the user for checking his verification code Commented Feb 25, 2014 at 16:56
  • 1
    @dandavis I whole heartedly disagree with in memory stores in production like you are suggesting. 1) Globals are evil and not dependable — we all know that 2) Memory will be localized to the process and therefore you can only scale vertically, not horizontally, which is one of the TRUE beauties of node. 3) What happens when the process goes down? All users will be logged out? A redis store is much more fault tolerant and robust then a global and will help you scale horizontally when ready. Commented Feb 25, 2014 at 17:54
  • 1
    @dandavis You missed the point. Its not HOW MUCH memory the solution uses, but HOW it uses that memory. Quite simply, you're solution will not scale outside a single process and therefore is not a production quality solution. I would certainly do that in development to KISS, but NEVER would you see an enterprise solution scoped to a single process. My guess is you don't maintain a production node stack. Commented Feb 25, 2014 at 18:14
  • 1
    Tend to agree with @jibsales - on this. The way Node is set up you certainly can use a variable to hold some data between requests, but that's just bad practice, not only in Node, but in javascript and just about any language in general. It's not very reliant to use variables for this, and suddenly someone decides to try out clusters or a multitude of other things that Node support that would lead to a miserable failure. The way to go is to use a database for this, not matter how tiny the piece of data you're going to store is. Commented Feb 25, 2014 at 20:26

1 Answer 1

1

I would say you're thinking the right way, and just need a short guide to get to do it. Sessions are the way to go here, and although you could store those anywhere, I'd suggest you start with basic in-memory sessions for now.

I found the following guide helpful: http://blog.modulus.io/nodejs-and-express-sessions. Just take it as far as Redis and not further, and you'll be well on your way.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.