node自定义web服务器 // 模块加载const http = require('http');const url = require("url");const fs = require("fs");// 初始变量声明const hostname = "127.0.0.1";const port = 3000;const server = http.createServer((req, res) = { le
// 模块加载 const http = require('http'); const url = require("url"); const fs = require("fs"); // 初始变量声明 const hostname = "127.0.0.1"; const port = 3000; const server = http.createServer((req, res) => { let urlObj = url.parse(req.url); let content = "Not Found!!"; if (fs.existsSync("www" + urlObj.pathname)) { content = fs.readFileSync("www" + urlObj.pathname); } // 返回指定URL的内容 res.end(content.toString()); }); server.listen(port, hostname, () => { console.log(`Server is running at http://${hostname}:${port}/`) });