http.createServer


const http = require('http'); // Node.js 內置的 http 模塊

/**

  • 請求事件發生時調用的回調函數 (Node.js 是執行單線程異步非阻塞事件驅動的)
    */
    function requestListener(req, rep) {
    console.log("Request received."); // 當我們啟動服務器訪問網頁時, 服務器可能會輸出兩次 'Request received.', 那是因為大部分瀏覽器都會在你訪問 http://localhost:8080/ 時嘗試讀取 http://localhost:8888/favicon.ico 站點圖標
    rep.writeHead(200, {'Content-Type':'text/html'}); // 響應頭
    rep.write('

    Hello World

    '); // 響應主體
    rep.end(); // 完成響應
    }

/**

console.log("Server has started.");


免責聲明!

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



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