macOS下搭建Node.js環境
阿新 • • 發佈:2019-01-02
1.安裝Homebrew
$:ruby -e "$(curl -fsSl http://raw.githubusercontent.com/Homebred/install/master/install);"
2.安裝Node
$:brew install node
需要稍微等待一會,看到如下介面即安裝成功
3.檢視Node版本:
$:node -v
測試:
在桌面新建一個test.js的檔案
$:cd Desktop/
$:touch test.js
$:vim test.js
寫入以下程式碼
var http = require('http' )
var data = {name:'stevin',location:'Beijing'};
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');
});
編寫完後儲存並且退出:wq
編輯狀態, 然後執行$:node Desktop/test.js
執行,顯示:
listening on localhost:8080
第一次系統會詢問是否同意接入網路, 點選同意即可,然後在瀏覽器位址列鍵入:localhost:8080, 即可顯示資料
{"name":"stevin","location":"Beijing"}