//引入系统模块http//创建网站服务器//为网站添加请求事件
const http = require('http');
const url = require('url');
const app = http.createServer();app.on('request', (req, res) => {//获取请求方式const method = req.method.toLowerCase();//获取请求地址const pathname = url.parse(req.url).pathname;res.writeHead(200, {'content-type': 'text/html;charset=utf8'});if (method == 'get') {if (pathname == '/' || pathname == 'index') {res.end('欢迎来到首页')} else if (pathname == '/list') {res.end('欢迎来到列表页')} else {res.end('你访问的页面不存在')}} else if (method == 'post') {}});
app.listen(3000);
console.log('服务器启动成功');
运行结果