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.
- Open IntroAjaxNodeJsServer/Exercises/server.js in your editor.
- Copy the existing HelloWorld response route and paste it as a new response route right below it.
- Name the new route NodeExercise.
- Define two params - fname and lname - and give them default values.
- Change the output to respond with "Hello First Last!" (or the appropriate values, if the parameters exist).
- Navigate to IntroAjaxNodeJsServer/Exercises/ in the command prompt/terminal and type npm install to install any needed Node.js modules.
- Type npm start to start the Node.js server.
- 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
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.