concept express.js in category node

This is an excerpt from Manning's book Get Programming with Node.js.
Unit 2 introduces you to Express.js, a web framework that most Node.js developers use to build applications. You learn what Express.js offers, how it works, and what you can do to customize it. In this unit, you also learn about the model-view-controller (MVC) application architecture pattern.
Lesson 9. Routing in Express.js
In lesson 8, I introduced Express.js as a framework for Node.js web applications. The rest of this unit is dedicated to exploring Express.js functionality and using its convenient methods. This lesson covers routing and how a few more Express.js methods allow you to send meaningful data to the user before building a view. You also walk through the process of collecting a request’s query string. The lesson ends by touching on the MVC design pattern.
Figure 9.3 shows Express.js as a layer over your application that handles requests but also feeds your application’s controllers. The callbacks decide whether a view should be rendered or some data should be sent back to the client.
In lesson 8, you constructed your first Express.js application, consisting of a route handling GET requests to your home-page URL. Another way to describe this route is as an application endpoint that takes an HTTP method and path (URL). Routes in Express.js should look familiar to you because you built the same routing structure at the end of unit 1. In Express.js, a route definition starts with your app object, followed by a lowercase HTTP method and its arguments: the route path and callback function.
A route handling POST requests to the /contact path should look like the following listing. This example uses the post method provided by Express.js.
Express.js lets you write routes with parameters in the path. These parameters are a way of sending data through the request. (Another way is with query strings, which I talk about at the end of this lesson.) Route parameters have a colon (:) before the parameter and can exist anywhere in the path. Listing 9.2 shows an example of a route with parameters. The route in this listing expects a request made to /items/ plus some vegetable name or number. A request to "/items/lettuce", for example, would trigger the route and its callback function. The response sends the item from the URL back to the user through the params property of the request object.
Listing 9.2. Using route parameters to indicate vegetable type in main.js
app.get("/items/:vegetable", (req, res) => { #1 res.send(req.params.vegetable); });Initialize a new project called express_routes, install Express.js, and add the code to require and instantiate the Express.js module. Then create a route with parameters, and respond with that parameter as shown in listing 9.2. At this point, your main.js should look like the code in the next listing.