Mac下nodeJS初體驗
這兩天博主出門在外,抽空體驗一下大名鼎鼎的node
安裝
brew install node
安裝測試
$ node -v
v8.4.0
運行本地腳本
用文本編輯器編輯一段js腳本,測試提供的Javascript環境
helloworld.js
$ node helloworld.js
Hello World
3
用node命令也可以進入交互式shell模式。
helloworld
helloworld.js
// 載入http模塊
var http = require("http")
// 創建服務器
http.createServer(function(request, response){
//發送HTTP頭部
//HTTP狀態:200:OK
//內容類型:text/plain
response.writeHead(200,{'Content-Type': 'text/plain'});
//發送響應數據
response.end("Hello World!");
}).listen(8000); //服務器在8000端口監聽
//終端打印信息
console.log("Server running at http://127.0.0.1:8000/");
用node運行腳本后,在本地瀏覽器訪問http://127.0.0.1:8000/就可以看到helloworld了。
初體驗先到此為止,開發工具的話推薦VScode