20.4. Express - First Look and Feel

Express is a web framework for Node.js. It is kind of the framework. There are others, most building on top it. In our context it is a choice to use Express to privide you with maximum versatility. We have seen several node functionalities not directly making web pages. File, and database i/o, REST, to name some. The Node.js depends heavily on ad hoc require ed modules. Express is ine of them, the most important, one could say.

20.4.1. Express Starts at the Command Line

Express has an idiosyncracy of what a normal web project architecturally should look like. The best practice for native Node.js that we have used so far has been modelled after this. Express has a command line tool that implements best practice architecture. Let us install it as follows from the command line. Remember that the -g option, global, makes the tools globally available, ie across projects. These -g option commands must be executed with administrator privileges.

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

is invokation via the Express Command Line Tool.

Example 20.1. The Express Command Line Tool
─$ npx express-generator -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

This result may also be visualized by

Example 20.2. Syntax Example
─$ npx express-generator -v pug --git firstlocal

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

   change directory:
     $ cd firstlocal

   install dependencies:
     $ npm install

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

With that under our belt, we shall proceed to see the way we normally build node applications.