Express 文檔(Express生成器)

Express應用程序生成器

使用應用程序生成器工具express-generator快速創建應用程序框架。

express-generator包安裝了express命令行工具,使用以下命令執行此操作:

$ npm install express-generator -g

使用-h選項顯示命令選項:

$ 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

例如,以下內容創建名爲myapp的Express應用程序,該應用程序將在當前工作目錄中創建在名爲myapp的文件夾中,並且視圖引擎將設置爲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

然後安裝依賴項:

$ cd myapp
$ npm install

在MacOS或Linux上,使用以下命令運行應用程序:

$ DEBUG=myapp:* npm start

在Windows上,使用此命令:

> set DEBUG=myapp:* & npm start

然後在瀏覽器中加載http://localhost:3000/以訪問該應用程序。

生成的應用程序具有以下目錄結構:

.
├── 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
生成器創建的應用程序結構只是構建Express應用程序的衆多方法之一,隨意使用此結構或修改它以最好地滿足你的需求。

上一篇:Hello world

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章