当前位置:网站首页>1-10 根据不同的url响应客户端的内容
1-10 根据不同的url响应客户端的内容
2022-06-30 21:26:00 【画不完的饼】
根据不同的url响应客户端的内容
const http = require('http')
// **1. 导入http模块**
// **2. 创建web服务器实例**
const server = http.createServer()
// **3. 为服务器实例绑定request事件,监听客户端的请求**
server.on('request', (req, res) => {
//获取请求的url地址
const url = req.url
//设置默认的响应内容为404 not found
let content = '<h1>404 not found</h1>'
//判断用户的请求是否为 /或者index.html首页
//判断用户的请求是否为 /about.html
if (url == '/' || url == '/index.html') {
content = '<h1>首页</h1>'
} else if (url == '/about.html') {
content = '<h1>关于</h1>'
}
//设置 Content-type 响应头,防止中文乱码
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')
})
边栏推荐
- Clickhouse distributed table engine
- Test medal 1234
- 元宇宙可能成为互联网发展的新方向
- Random talk about Clickhouse join
- Understanding polymorphism
- Metauniverse may become a new direction of Internet development
- Export El table as is to excel table
- “信任机器”为发展赋能
- pytorch geometric torch-scatter和torch-sparse安装报错问题解决
- 防范未授权访问攻击的十项安全措施
猜你喜欢
随机推荐
Export El table as is to excel table
. NETCORE redis geo type
Encryption and decryption and the application of OpenSSL
双立体柱状图/双y轴
Is it safe to open an account for stock trading on mobile phones?
开源实习经验分享:openEuler软件包加固测试
Clickhouse distributed table engine
Understand what MySQL index push down (ICP) is in one article
根据肠道微生物组重新思考健康饮食
个人开发的渗透测试工具Satania
升级kube出现unknown flag: --network-plugin
A group of K inverted linked lists
CA I ah, several times Oh, ah, a sentence IU home Oh
sdfsdf
Five years after graduation, I wondered if I would still be so anxious if I hadn't taken the test
Ssh server configuration file parameter permitrootlogin introduction
Multi table operation - foreign key constraint
Random talk about Clickhouse join
Internet of things botnet gafgyt family and backdoor vulnerability exploitation of Internet of things devices
针对美国国家安全局“酸狐狸”漏洞攻击武器平台的分析与应对方案建议








