Skip to content

Latest commit

 

History

History
38 lines (28 loc) · 976 Bytes

8.md

File metadata and controls

38 lines (28 loc) · 976 Bytes

http模块,根据不同的url响应不同的html内容

[toc]

冬天响应内容

const http = require('http')
const server = http.createServer()

server.on('request', (req, res) => {
//1. 获取请求的url地址,和客户端有关使用req获取
const url = req.url

//2.设置响应内容为404 not found
let content = '<h1>404 not found</h1>'

//3.判断用户请求是否为 / 或者 /index.html首页
//4.判断用户请求是否为 /about.html关于页面
if (url === '/' || url === '/index.html'){
    content = '<h1>首页</h1>'
}else if (url === '/about.html'){
    content = '<h1>关于页面</h1>'
}

//5.设置content-type响应头,防止中文乱码
res.setHeader('Content-Type','text/html;charset=utf-8')

//6.使用res.end()把相应内容给客户端
res.end(content)
})    

server.listen(80, () => {
    console.log('server runing ar http://127.0.0.1')
}) 

image-20220425183019747