//创建网站服务器模板
const http = require('http');
const url = require('url');
//网站服务器对象
const app = http.createServer();
const path = require('path');
const fs = require('fs');
//客户端有请求
/* const querystring = require('querystring'); */
app.on('request', (req, res) => {//获取请求路径console.log(url.parse(req.url).pathname);let pathname = url.parse(req.url).pathname;let realPath = path.join(__dirname, 'public' + pathname);fs.readFile(realPath, (error, result) => {if (error != null) {res.writeHead(404, {'content-type': 'text/html;charset=utf8'})res.end('文件读取失败');return;}res.end(result);})});
//监听端口
app.listen(3000);
console.log('服务器启动成功');
运行结果