centos 下 搭建 nodejs 开发环境
[*]下载node并初始化
从 http://nodejs.org/download/ 下载最新的nodejs压缩包
1
2
3
wget http://nodejs.org/dist/v0.10.32/node-v0.10.32-linux-x64.tar.gz
tar -zxvf node-v0.10.32-linux-x64.tar.gz
cd node-v0.10.32-linux-x64
设置node到系统路径
1
vim /etc/profile
在文件结尾添加:
1
2
export NODEJS_HOME=/home/develop/nodjs/node-v0.10.32-linux-x64
export PATH=$PATH:$NODEJS_HOME/bin
退出后更新当前执行环境
1
source /etc/profile
再执行
1
node -v和 npm -v
如果能正常看到版本号
1
v0.10.32 和 1.4.28
说明node和npm环境配置成功(npm 是nodejs包管理器)
[*]安装框架 express
1
$ mkdir myapp $ cd myapp
[*]通过命令创建package.json
1
$ npm init
安装express 到应用并添加到依赖列表
1
$ npm install express --save
[*]使用 Express application generator 快速构建express应用
[*]安装
$ npm install express-generator -g
[*]查看帮助
1
2
3
4
5
6
7
8
9
10
11
12
13
$ express -h
Usage: express
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) (defaults to plain css)
-f, --force force on non-empty directory
[*]使用 generator 创建应用并初始化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
$ 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
install dependencies:
$ cd myapp && npm install
run the app:
$ DEBUG=myapp ./bin/www
页:
[1]