const http = require('http') const server = http.createServer() server.on('request',(req,res)=>{ let content = '<h1>404 Not found!</h1>' const url = req.url const method = req.method if(url === '/' || url ==='/index.html'){ content = '<h1>首页</h1>' }else if(url === '/about.html'){ content = '<h1>关于页面</h1>' } // res.setHeader()设置响应头,解决中文乱码问题 res.setHeader('Content-Type','text/html;charset=utf-8') // 调用res.end方法,向客户端响应内容 向客户端发生指定内容,并结束这次请求 res.end(content) }) server.listen(80,()=>{ console.log('server running at http://127.0.0.1') })
标签:web,const,url,res,http,server,content,服务器 From: https://www.cnblogs.com/wencaiguagua/p/16815156.html