
Node.js,很火吧,火的很。爱折腾,爱前沿的我肯定不能落下队伍,今天我就来分享下如何在本地架设Node.js的环境。
何为Node.js
Node.js is a platform built on Chrome’s JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.
这是官网上的一段介绍。
还有一篇很好的博文详细介绍了Node,大家可以看看。Node.js 究竟是什么?
安装Node.js
在Mac Lion下,我还是用简单的Homebrew的包安装方法来安装,不会Homebrew的看这里。Mac开发者利器-Homebrew介绍及安装
安装完成node,会提示你没有安装npm,并告诉你具体的安装办法。
1
2
3
4
5
6
7
| Homebrew has NOT installed npm. We recommend the following method of
installation:
curl http://npmjs.org/install.sh | sh
After installing, add the following path to your NODE_PATH environment
variable to have npm libraries picked up:
/usr/local/lib/node_modules |
安装npm。
1
| $ curl http://www.npmjs.org/install.sh | sh
|
安装成功
1
2
3
| /usr/local/bin/npm -> /usr/local/lib/node_modules/npm/bin/npm-cli.js
npm@1.1.0-3 /usr/local/lib/node_modules/npm
It worked
|
设置NODE_PATH环境变量
1
| $ export NODE_PATH="/usr/local/lib/node_modules/"
|
测试
1
2
3
4
5
6
7
8
| var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(8888);
console.log('Server running at http://127.0.0.1:8888/');
|
1
2
| $ node example.js
Server running at http://127.0.0.1:8888/
|
测试成功

安装express框架
1
2
3
| $ npm install -g express
$ npm install -g express-generator
$ express -V
|
新建一个Node server文件夹~/node_server
1
2
3
| $ cd ~
$ mkdir node_server
$ cd node_server
|
创建项目文件夹nodetest
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| create : nodetest
create : nodetest/package.json
create : nodetest/app.js
create : nodetest/public
create : nodetest/routes
create : nodetest/routes/index.js
create : nodetest/views
create : nodetest/views/layout.jade
create : nodetest/views/index.jade
create : nodetest/public/javascripts
create : nodetest/public/images
create : nodetest/public/stylesheets
create : nodetest/public/stylesheets/style.css
dont forget to install dependencies:
$ cd nodetest && npm install
|
安装npm
1
2
| $ cd nodetest
$ npm install
|
1
2
| $ node app.js
$ npm start |
安装成功,浏览器输入http://127.0.0.1:3000/
,即可看到效果。
npm中有许多我们需要的库,比如:
1
2
| $ npm install jquery
$ npm install yui
|