Explore the core principles of clean Node.js backend development with practical scenarios covering authentication, database operations, emailing, payment, and real-time features.
Which of the following best describes the process for securely registering a user and issuing a login token in a Node.js API?
Explanation: The correct practice is to hash the user's password before saving and issue a signed JWT upon successful login for session management. Storing passwords in plain text is insecure. Only checking email ignores password validation, and issuing tokens without credential checks creates security risks.
What is the main purpose of using model methods like 'create', 'find', 'findByIdAndUpdate', and 'findByIdAndDelete' in backend CRUD routes?
Explanation: These model methods execute database operations vital for CRUD functionality. Rendering frontend interfaces is unrelated. User input validation requires additional logic, and these methods do not handle data encryption by default.
Which setup is required to send an email using Node.js and a common email-sending library?
Explanation: A transporter must be configured with email credentials to connect and send messages using functions like sendMail. Simply storing emails or only confirming receipt does not send email. Hashing content is unnecessary unless securing sensitive information.
When building a payment endpoint in Node.js, what vital step is necessary to create a secure payment intent?
Explanation: Secure payment processing requires initializing the payment provider's client (like Stripe) with a private key and requesting an intent from their API. Card details should not be stored directly, sensitive keys should be kept safe, and token generation should be handled by the provider.
How does a basic chat server using WebSockets in Node.js broadcast messages to all connected clients?
Explanation: Broadcasting in real-time chat involves listening for incoming message events and emitting them to all connected clients with a new event. Logging, file writing, or sending back just to the sender do not distribute messages to other users.