您的位置:首页 > Web前端 > Node.js

nodejs Express快速创建应用程序

2017-09-02 14:22 309 查看

Express 应用程序生成器

假如您看了node.js开发指南,根据书山的讲解,您安装express之后会发现报错,找不到express模块;因为那是很久以前的node版本,因此,您要换新的方式来安装。

使用以下命令安装 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
-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 应用程序:

$ 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: