Express Tutorial Part 2: Creating a skeleton website – Learn web development | MDN

This file creates an express application object (named app, by convention), sets up the application with various settings and middleware, and then exports the app from the module. The code below shows just the parts of the file that create and export the app object:

const

express

=

require

(

"express"

)

;

const

app

=

express

(

)

;

module

.

exports

=

app

;

Back in the www entry point file above, it is this module.exports object that is supplied to the caller when this file is imported.

Let’s work through the app.js file in detail. First, we import some useful node libraries into the file using require(), including http-errors, express, morgan and cookie-parser that we previously downloaded for our application using npm; and path, which is a core Node library for parsing file and directory paths.

const

createError

=

require

(

"http-errors"

)

;

const

express

=

require

(

"express"

)

;

const

path

=

require

(

"path"

)

;

const

cookieParser

=

require

(

"cookie-parser"

)

;

const

logger

=

require

(

"morgan"

)

;

Then we require() modules from our routes directory. These modules/files contain code for handling particular sets of related “routes” (URL paths). When we extend the skeleton application, for example to list all books in the library, we will add a new file for dealing with book-related routes.

const

indexRouter

=

require

(

"./routes/index"

)

;

const

usersRouter

=

require

(

"./routes/users"

)

;

Note: At this point, we have just imported the module; we haven’t actually used its routes yet (this happens just a little bit further down the file).

Next, we create the app object using our imported express module, and then use it to set up the view (template) engine. There are two parts to setting up the engine. First, we set the ‘views‘ value to specify the folder where the templates will be stored (in this case the subfolder /views). Then we set the ‘view engine‘ value to specify the template library (in this case “pug”).

const

app

=

express

(

)

;

app

.

set

(

"views"

,

path

.

join

(

__dirname

,

"views"

)

)

;

app

.

set

(

"view engine"

,

"pug"

)

;

The next set of functions call app.use() to add the middleware libraries that we imported above into the request handling chain.
For example, express.json() and express.urlencoded() are needed to populate req.body with the form fields.
After these libraries we also use the express.static middleware, which makes Express serve all the static files in the /public directory in the project root.

app

.

use

(

logger

(

"dev"

)

)

;

app

.

use

(

express

.

json

(

)

)

;

app

.

use

(

express

.

urlencoded

(

{

extended

:

false

}

)

)

;

app

.

use

(

cookieParser

(

)

)

;

app

.

use

(

express

.

static

(

path

.

join

(

__dirname

,

"public"

)

)

)

;

Now that all the other middleware is set up, we add our (previously imported) route-handling code to the request handling chain. The imported code will define particular routes for the different parts of the site:

app

.

use

(

"/"

,

indexRouter

)

;

app

.

use

(

"/users"

,

usersRouter

)

;

Note: The paths specified above ('/' and ‘/users') are treated as a prefix to routes defined in the imported files.
So for example, if the imported users module defines a route for /profile, you would access that route at /users/profile. We’ll talk more about routes in a later article.

The last middleware in the file adds handler methods for errors and HTTP 404 responses.

 
app

.

use

(

(

req

,

res

,

next

)

=>

{

next

(

createError

(

404

)

)

;

}

)

;

app

.

use

(

(

err

,

req

,

res

,

next

)

=>

{

res

.

locals

.

message

=

err

.

message

;

res

.

locals

.

error

=

req

.

app

.

get

(

"env"

)

===

"development"

?

err

:

{

}

;

res

.

status

(

err

.

status

||

500

)

;

res

.

render

(

"error"

)

;

}

)

;

The Express application object (app) is now fully configured. The last step is to add it to the module exports (this is what allows it to be imported by /bin/www).

module

.

exports

=

app

;