1、mac node.js環(huán)境的配置
第一步:打開終端库倘,輸入以下命令安裝Homebrew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Homebrew官網(wǎng) http://brew.sh/index_zh-cn.html
Homebrew 安裝成功.png
第二步:安裝node,在終端輸入以下命令
brew install node
安裝 node.png
第三步:查看node安裝是否成功
查看node版本.png
2庸队、新建測試程序
第一步:新建一個文件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');
});
第二步:用終端找到其所在的文件目錄運行
node Desktop/test.js listen on localhost:8080
第三步:通過瀏覽器進行訪問徙缴,返回json格式的數(shù)據(jù)
瀏覽器訪問.png
第四步:前端就可以通過這個接口進行數(shù)據(jù)解析了辜王。