Mac下nodeJS初體驗


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM