Express-generator

Author Avatar
cuteximi 7月 05, 2017
  • 在其它设备中阅读本文章

Express 应用生成器

通过应用生成器工具 express 可以快速创建一个应用的骨架。

1.通过如下命令安装:
$ 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)
        --hbs           add handlebars engine support
    -H, --hogan         add hogan.js engine support
    -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
2.下面的示例就是在当前工作目录下创建一个命名为 myapp 的应用。
$ express myapp

  warning: the default view engine will not be jade in future releases
  warning: use `--view=jade' or `--help' for additional options


   create : myapp
   create : myapp/package.json
   create : myapp/app.js
   create : myapp/public
   create : myapp/routes
   create : myapp/routes/index.js
   create : myapp/routes/users.js
   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
   create : myapp/public/images
   create : myapp/public/javascripts
   create : myapp/public/stylesheets
   create : myapp/public/stylesheets/style.css

   install dependencies:
     $ cd myapp && npm install

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

3.然后安装所有依赖包:
$ cd myapp 
$ npm install
4.启动这个应用

MacOS 或 Linux 平台:

$ DEBUG=myapp npm start

Windows 平台使用如下命令:

set DEBUG=myapp & npm start
5.然后在浏览器中打开 http://localhost:3000/ 网址就可以看到这个应用了。
6.通过 Express 应用生成器创建的应用一般都有如下目录结构:
.
├── 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

This blog is under a CC BY-NC-SA 3.0 Unported License
本文链接:http://blog.cuteximi.com/express-generator/