Express document (express generator)

Express application builder

Use the express generator tool to quickly create an application framework.

The express generator package installs the express command-line tool, using the following command:

$ npm install express-generator -g

Use the - h option to display command options:

$ express -h

  Usage: express [options] [dir]

  Options:

    -h, --help          output usage information
        --version       output the version number
    -e, --ejs           add ejs engine support
        --hbs           add handlebars engine support
        --pug           add pug engine support
    -H, --hogan         add hogan.js engine support
        --no-view       generate without view engine
    -v, --view <engine> add view <engine> support (ejs|hbs|hjs|jade|pug|twig|vash) (defaults to jade)
    -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

For example, the following creates an Express application named myapp, which will be created in the current working directory in a folder named myapp, and the view engine will be set to Pug:

$ express --view=pug myapp

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

Then install the dependencies:

$ cd myapp
$ npm install

On MacOS or Linux, run the application using the following command:

$ DEBUG=myapp:* npm start

On Windows, use this command:

> set DEBUG=myapp:* & npm start

Then load http://localhost:3000 / in the browser to access the application.

The generated application has the following directory structure:

.
├── 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

The application structure created by the generator is just one of the many ways to build an Express application. Feel free to use it or modify it to best meet your needs.

Previous: Hello world

Tags: Javascript npm JSON Handlebars less

Posted on Sun, 01 Dec 2019 06:49:37 -0800 by Brokenhope