joe
joe

Reputation: 9484

How can you add route to Nodejs without restarting the server?


I am searching for a way to create a "Forgot password" page for my users.

It must have the following features:

  1. URLs must be unique and random for individual user. I decided to use a hash function for this.
  2. URLs must expire after users finish obtaining a new password or after a time-out period has ended.
  3. Web-server cannot be changed by this operation.

Webserver : Node.js 5.2.0
Module : Expressjs 4.13.3

In my understanding, Node.js needs to restart itself every time when I add a new/remove route. Currently, my colleague uses nodemon. Although it is a good application, it is not ideal for a "forgot password" page due to the fact that it restarts the web-server each time it runs.

Question:

  1. What is the best technique for this scenario?
  2. What is a specific keyword for this question?

Upvotes: 2

Views: 382

Answers (2)

Jason Livesay
Jason Livesay

Reputation: 6377

Node will only need to be restarted after you change the actual code for your Forgot Password page, not every time someone clicks on Forgot Password. "Adding a route" means adding the code in your app that handles that forgot_password link.

Upvotes: 1

whitfin
whitfin

Reputation: 4629

You can add routes using path parameters in Node (I assume you're using Express.js, but if you clarify I can modify this answer).

In this case you would register a route like this:

/api/v1/user/forgotPasswordEndpoint/:unique_hash

This would match things such as

/api/v1/user/forgotPasswordEndpoint/12345

You can access 12345 through req.params.unique_hash. This lets you treat that endpoint differently based on the hash, and grants you access to the hash in your code so you can do something with it.

Upvotes: 3

Related Questions