使用應用程序生成器工具express-generator
快速建立應用程序框架。javascript
express-generator
包安裝了express命令行工具,使用如下命令執行此操做:css
$ npm install express-generator -g
使用-h
選項顯示命令選項:java
$ 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:git
$ 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
而後安裝依賴項:express
$ cd myapp $ npm install
在MacOS或Linux上,使用如下命令運行應用程序:npm
$ DEBUG=myapp:* npm start
在Windows上,使用此命令:json
> set DEBUG=myapp:* & npm start
而後在瀏覽器中加載http://localhost:3000/
以訪問該應用程序。segmentfault
生成的應用程序具備如下目錄結構:瀏覽器
. ├── 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應用程序的衆多方法之一,隨意使用此結構或修改它以最好地知足你的需求。