20.7. Express Generating a Dynamic Website

Normally, probably, we generate dynamic sites. This is what most web development is about. Doing that we must reference a templating engine that is capable of interpolating variable dynamic data into an HTML5 template. Because there is a choice of templating engines we denominate one, pug. The default is, strangely, an ancestor of pug, jade that nobody uses any more.

$ npx express-generator -v pug --git firstlocal

   create : nodeOne/
   create : nodeOne/public/
   create : nodeOne/public/javascripts/
   create : nodeOne/public/images/
   create : nodeOne/public/stylesheets/
   create : nodeOne/public/stylesheets/style.css
   create : nodeOne/routes/
   create : nodeOne/routes/index.js
   create : nodeOne/routes/users.js
   create : nodeOne/views/
   create : nodeOne/views/error.pug
   create : nodeOne/views/index.pug
   create : nodeOne/views/layout.pug
   create : nodeOne/.gitignore
   create : nodeOne/app.js
   create : nodeOne/package.json
   create : nodeOne/bin/
   create : nodeOne/bin/www

   change directory:
     $ cd nodeOne

   install dependencies:
     $ npm install

   run the app:
     $ DEBUG=nodeone:* npm start

Giving us the skeleton infrastructure

$ tree firstlocal
firstlocal
├── app.js
├── bin
│   └── www
├── package.json
├── public
│   ├── images
│   ├── javascripts
│   └── stylesheets
│   └── style.css
├── routes
│   ├── index.js
│   └── users.js
└── views
    ├── error.pug
    ├── index.pug
    └── layout.pug

7 directories, 9 files

It is recommended to adapt the package file:

Example 20.8. The Package File, package.json (fragment)
...
  "scripts": {
    "start": "node ./bin/www",
    "pretest": "npm install",
    "test": "DEBUG=firstlocal:* node ./bin/www"
  },
...

upon which the server may be started for test with npm test

Having done that we will set it up to use git as a matter of good habit (abbreviated)

$ git init --initial-branch=main
$ mkdir controllers
$ mkdir models
$ git add .
$ git commit -m 'initial'

The package.json

{
  "name": "nodeone",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www"
  },
  "dependencies": {
    "cookie-parser": "~1.4.4",
    "debug": "~2.6.9",
    "express": "~4.16.1",
    "http-errors": "~1.6.3",
    "morgan": "~1.9.1",
    "pug": "2.0.0-beta11"
  }
}

To test, we do

$ cd nodeOne
$ npm install
npm WARN deprecated core-js@2.6.12: core-js@<3.4 is no longer maintained and not recommended for usage due to the number of issues. Because of the V8 engine whims, feature detection in old core-js versions could cause a slowdown up to 100x even if nothing is polyfilled. Please, upgrade your dependencies to the actual version of core-js.

added 124 packages, and audited 125 packages in 6s

8 packages are looking for funding
  run `npm fund` for details

4 vulnerabilities (2 low, 2 high)

To address issues that do not require attention, run:
  npm audit fix

To address all issues, run:
  npm audit fix --force

Run `npm audit` for details.

$ npm start

The browser is then pointed at the designated port, and we see welcome screen.

Figure 20.3. The resulting Express Site as Expected
The resulting Express Site as Expected

Notice the given URL