Express and Node.js Training from StrongLoop

该文件可能是相对于英文的文档已经过时。有关最新更新,请以英文的文档

This document might be outdated relative to the documentation in English. For the latest updates, please refer the documentation in English.

Express应用生成器

使用应用生成器express快速创建一个应用程序框架。

你可以使用以下命令来安装:

$ npm install express-generator -g

使用 -h 选项来显示命令的选项列表:

$ express -h

  Usage: express [options] [dir]

  Options:

    -h, --help          output usage information     输出使用说明
    -V, --version       output the version number    输出版本号
    -e, --ejs           add ejs engine support (defaults to jade)  添加ejs引擎支持(默认为jade)
        --hbs           add handlebars engine support              添加handlebars引擎支持
    -H, --hogan         add hogan.js engine support                添加hogan.js引擎支持
    -c, --css <engine>  add stylesheet <engine> support (less|stylus|compass) (defaults to plain css)  添加样式表预处理引擎支持(less|stylus|compass)
    -f, --force         force on non-empty directory               强制在非空的目录生成应用框架

举个例子,下面的命令可以在当前目录创建一个名为 myapp 的Express应用。

$ express 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.jade
   create : myapp/views/layout.jade
   create : myapp/views/error.jade
   create : myapp/bin
   create : myapp/bin/www

接下来安装依赖包:

$ cd myapp 
$ npm install

运行程序 (MacOS或Linux):

$ DEBUG=myapp ./bin/www

在Windows上,你需要运行这条命令:

> set DEBUG=myapp & node .\bin\www

接下来在浏览器中打开 http://localhost:3000/ 来访问应用。

生成的应用目录结构大概是这样的:

.
├── app.js
├── bin
│   └── www
├── package.json
├── public
│   ├── images
│   ├── javascripts
│   └── stylesheets
│       └── style.css
├── routes
│   ├── index.js
│   └── users.js
└── views
    ├── error.jade
    ├── index.jade
    └── layout.jade

7 directories, 9 files

应用生成器生成的应用结构仅仅是众多Express应用结构中的一种,你也可以选择不使用它或者做一些修改来最好地适应你的需要。