Adding a New Route to a Node.js Application - Exercise

Contact Us or call 1-877-932-8228
Adding a New Route to a Node.js Application - Exercise

Adding a New Route to a Node.js Application

Duration: 10 to 15 minutes.

In this exercise, you will modify our Node.js application to add a new response.

  1. Open IntroAjaxNodeJsServer/Exercises/server.js in your editor.
  2. Copy the existing HelloWorld response route and paste it as a new response route right below it.
  3. Name the new route NodeExercise.
  4. Define two params - fname and lname - and give them default values.
  5. Change the output to respond with "Hello First Last!" (or the appropriate values, if the parameters exist).
  6. Navigate to IntroAjaxNodeJsServer/Exercises/ in the command prompt/terminal and type npm install to install any needed Node.js modules.
  7. Type npm start to start the Node.js server.
  8. Test your solution in a browser by visiting http://localhost:8080/NodeExercise



We copied the HelloWorld route and pasted it as a new route, renaming the route to NodeExercise so that the user can visit http://localhost:8080/NodeExercise in a browser. We define two parameters fname and lname, reading them from the request if they exist or giving them default values if not. We change the displayed response to include both parameters.