一、mac node.js环境的配置
1、打开终端,输入以下命令安装Homebrew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
可以直接进入Homebrew官网 http://brew.sh/index_zh-cn.html
2、安装node,在终端输入以下命令
brew install node
3、查看node安装是否成功
二、测试是否node可用
1、新建一个文件test.js
var http = require('http');
var data = {key : 'value', hello: 'world'};
var srv = http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(data));
});
srv.listen(8080, function() {
console.log('listening on localhost:8080');
});
2、用终端找到其所在的文件目录运行
node test.js listen on localhost:8080
3、通过浏览器(localhost:8080)进行访问,会返回如下json格式的数据,即为成功