express 4.x以后將express命令獨立到 express-generator包中,所以想使用express初始化項目目錄,可以npm install express-genrator
1
|
$ npm install express-generator -g
|
-g 選項是指安裝到全局
查看express命令選項
1
2
3
4
5
6
7
8
9
10
11
12
13
|
$ express -h
Usage: express [options] [dir]
Options:
-h, --help 查看幫助信息
-V, --version 查看版本
-e, --ejs 增加ejs模板引擎 (默認模板引擎是jade)
--hbs 增加 handlebars 引擎支持
-H, --hogan 增加 hogan.js引擎支持
-c, --css <engine> 增加 stylesheet 引擎支持(less|stylus|compass) (默認使用css)
-f, --force 如果沒有該目錄則創建
|
比如初始化myapp這個項目目錄
1
2
|
$ express myapp
|
會在該目錄下生成這些文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
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
install dependencies:
$ cd myapp && npm install
run the app:
$ DEBUG=myapp ./bin/www
|
目前新版的express命令會將啟動命令放到bin下面www腳本中
./bin/www 執行即可啟動node.js,而且會有個默認頁面
如果啟動報如下錯誤,listen EADDRINUSE 是端口被占用,殺掉其他node進程啟動即可
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
ivon@IvonMac /data/code/node/myapp$node ./bin/www
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE
at errnoException (net.js:904:11)
at Server._listen2 (net.js:1042:14)
at listen (net.js:1064:10)
at Server.listen (net.js:1138:5)
at Function.app.listen (/data/code/node/myapp/node_modules/express/lib/application.js:556:24)
at Object.<anonymous> (/data/code/node/myapp/bin/www:7:18)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
|