Getting started with NodeJS and Express - Setting up routes

This is the second article in a series. See the previous article.
Once you have a basic server set up using Express, the next step is to add a router and some routes.
First, some background...
The request and response messages
Every time you type in an HTTP website address, you send an HTTP request message out to a server for resources, and the server sends an HTTP response message back that (hopefully!) contains the requested resources.
The request message contains a method (the default method for browsers is "GET") and a URL endpoint. It can also contain a "body," or a payload of data for the server, and headers that contain additional information about the message.
The response message contains a status code that signals the result of the request (for example, 200 means the request was successful), status text (e.g., OK) and, like the request message, an optional body and headers.
The server has access to the request and response objects, and therefore access to the properties of these objects (i.e., the method, URL, status code, status text, body, and headers).
Routes
When we set routes for our server, we are setting the valid URL endpoints that a user can make requests to. We can also add logic to different route/method combinations to tell the server what to do with different types of requests.
To start, create a new folder in your backend called "routes." In this folder, create a file to store your routes - for my recipe app project, I called mine recipes.js.
In the router file, set up an instance of an Express Router - make sure to export it at the bottom so you can access the router in server.js.
const express = require("express")
const router = express.Router()
// Routes will go here!!
module.exports = router
Then back over to server.js, import the router at the top of the file.
const recipeRoutes = require("./routes/recipes")
While we're in server.js, we need to tell the server to use the router for all incoming requests to "/api/recipes." We set a global requirement via the built-in Express method .use(). The first argument is the API endpoint that all the router paths are relative to, and the second argument is the recipe router.
app.use("/api/recipes", recipeRoutes)
Now we'll add some routes to the recipes.js router. For my recipe app project, I started with some basic CRUD (Create, Retrieve, Update, and Delete) functionality. The express.Router class comes with built-in methods for these and other actions:
// CREATE a new recipe
router.post("/", createRecipe)
// RETRIEVE all recipes
router.get("/", getRecipes)
// RETRIEVE a single recipe
router.get("/:id", getRecipe)
// UPDATE a single recipe
router.patch("/:id", updateRecipe)
// DELETE a recipe
router.delete("/:id", deleteRecipe)
Each method above (get, post, delete, and patch) takes two arguments: a route (or endpoint) and a callback function that will be called when a request is made to the specified endpoint and method. Note that these endpoints are all relative to "/api/recipes."
We'll revisit the callback functions in my next post - for now, they're just descriptive names.
Two final notes...
Route parameters
Three of the above endpoints include a route parameter, :id. A parameter is signified by a colon. After the colon is the route parameter - this is where the user can provide information to the server. For example, the id of a specific resource the user wants to retrieve. The server has access to parameters through the request object: req.params will return an object with a key/value pair, where the key is equal to the router parameter (e.g., id) and the value is equal to whatever information the user specified in this location (e.g., 123).
Middleware
To be able to make full use of the router, we will need to require an Express built-in method, express.json(), that looks at all incoming requests, determines if the request has a body (data) attached, and, if so, parses the data to JSON format so that it can be used by our route callback functions later.
We will set this method as "middleware." Middleware is any code that runs between the server receiving the request and sending back the response.
In order to require this middleware on all the routes in our app, we use the express method .use() on our app.
So at this point, here is what our server.js file looks like:
const express = require("express")
const mongoose = require("mongoose")
const recipeRoutes = require("./routes/recipes")
// Create express app
const app = express()
const port = 4000
// Middleware
app.use(express.json())
// Routes
app.use("/api/recipes", recipeRoutes)

