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.");

posted @ 2020-10-16 13:03  ayuuuuuu  阅读(1180)  评论(0编辑  收藏  举报