Using routes with Express
Routes indicate the processing to be performed based on the requested URL. Compared to what we wrote when using the http
module of Node.js with the createServer(callback)
method, this consists of writing the content of the callback(req, res)
function according to the req
request received.
The routes are described in the app.js
file, which is the main file created by Express. Let’s examine its content.
The initial content of the app.js file
To understand how routes work in Express, open the app.js
file located in the main application directory, and you will see the content of this file, like this:
app.js file
var createError = require('http-errors'); var express = require('express'); var path = require('path'); var cookieParser = require('cookie-parser'); var logger = require('morgan'); var indexRouter = require('./routes/index'); var usersRouter = require('./routes/users&apos...