In lesson 4, I introduced you to the web server and showed how you can create one with Node.js. Every time a user visits a URL that leads to your application, a request is made, and each request must be processed by the code you write. In this lesson, you learn how to gather and process some of the information in these requests. You also build application routes—code logic to match requests with appropriate responses.
This lesson covers
- Collecting and processing request data
- Submitting a POST request with the curl command
- Building a web application with basic routes
Consider this
As you plan web pages for your recipe application, you realize that the basic web server you’ve built knows how to respond only with single lines of HTML. What if you want to show a complete home page and different HTML content for a contact page?
Every web application uses routes alongside its web server to ensure that users get to see what they specifically requested. With Node.js, you can define these routes in as few steps as any conditional block.
To start this lesson, rearrange the code from lesson 4 to get a better idea of how the server is behaving. Create a new project called second_server within its own project directory, and inside, add a new main.js file.
Note
In this lesson and following lessons, I expect you to initialize your Node.js application with npm init and to follow the guidance in lesson 4 to create a package.json file.