Mac OSX Lion上安装Node环境

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介绍及安装

1
$ brew install node

安装完成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
$ vi example.js
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
$ express 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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值