Express is a web framework for Node.js. It is kind of the framework. There are others, most building on top it, but it is natural that we use here. We have already seen several node functionalities not involving directly making web pages, eg file, and database i/o, REST, etc. Therefore Express is not globally installed on the development platform. Instead we install it ad hoc for needy projects. As an advocate of minimalism, let me first show you the minimal way to get started with Express, then I will demo the method commonly considered the normal best practice.
It is made part of a Node.js project by applying the following commands:
mkdir myexpapp cd myexpapp git init npm init -y npm install express
Resulting in
package.json
{
"name": "myexpapp",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
}
}
Look back at the section called “Project Creation - Best Practice”, and expand the above to
package.json
{
"name": "myexpapp",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"checkpoint": "git add . && git commit -F - && git push origin master ",
"pretest": "npm install",
"test": "node index"
},
"keywords": [],
"author": "Niels Müller Larsen <nmla@iba.dk> (http://dkexit.eu)",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
},
"devDependencies": {},
"description": ""
}
The minimal, proverbial express application
is then
"use strict";
const hostname = "localhost";
const port = Number(process.argv[2]) || 3000;
const express = require("express");
const app = express();
app.get("/", (req, res) => res.send("Hello World!"))
app.listen(port, () => console.log(`Server has started. Navigate to http://${hostname}:${port}/ to use it.`));
Now do npm test from your terminal,
and then
live from your's truly's localhost!
Express has an idiosyncracy of what a normal web project
architecturally should look like.
There's an Express command line tool that implements
this. Let us install it as follows. Remember that the
-g option, global, makes
the tools globally available, ie across projects.
npm install express-generator -g
If you have installed node as a computer wide installation,
not purely in your local user part of the file system, you may
need to be an administrator to achieve the required
permissions. On Linux or OSX this may be done with
sudo, on Windows you might need to run
cmd in administrator mode. Now, let us see what
is possible and what not.
express -h
Usage: express [options] [dir]
Options:
--version output the version number
-e, --ejs add ejs engine support
--pug add pug engine support
--hbs add handlebars engine support
-H, --hogan add hogan.js engine support
-v, --view <engine> add view <engine> support (dust|ejs|hbs|hjs|jade|pug|twig|vash) (defaults to jade)
--no-view use static html instead of view engine
-c, --css <engine> add stylesheet <engine> support (less|stylus|compass|sass) (defaults to plain css)
--git add .gitignore
-f, --force force on non-empty directory
-h, --help output usage information
With that done let us try to generate a skeleton web application.
express --view=pug demoproject
create : demoproject/
create : demoproject/public/
create : demoproject/public/javascripts/
create : demoproject/public/images/
create : demoproject/public/stylesheets/
create : demoproject/public/stylesheets/style.css
create : demoproject/routes/
create : demoproject/routes/index.js
create : demoproject/routes/users.js
create : demoproject/views/
create : demoproject/views/error.pug
create : demoproject/views/index.pug
create : demoproject/views/layout.pug
create : demoproject/app.js
create : demoproject/package.json
create : demoproject/bin/
create : demoproject/bin/www
change directory:
$ cd demoproject
install dependencies:
$ npm install
run the app:
$ DEBUG=demoproject:* npm start
With that under our belt, we shall proceed to see the way we normally build node applications.