当前位置:网站首页>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')
})
边栏推荐
- 文本生成模型退化怎麼辦?SimCTG 告訴你答案
- Analysis and proposal on the "sour Fox" vulnerability attack weapon platform of the US National Security Agency
- 双立体柱状图/双y轴
- ca i啊几次哦啊句iu家哦11111
- Understand what MySQL index push down (ICP) is in one article
- 《ClickHouse原理解析与应用实践》读书笔记(2)
- 笔记【JUC包以及Future介绍】
- 个人开发的渗透测试工具Satania
- 多态在代码中的体现
- Et la dégradation du modèle de génération de texte? Simctg vous donne la réponse
猜你喜欢
随机推荐
Ssh server configuration file parameter permitrootlogin introduction
What about degradation of text generation model? Simctg tells you the answer
asp. Net core JWT delivery
Clickhouse native monitoring item, system table description
遇到“word在试图打开文件时遇到错误”怎么办?
Apply for vector bus protocol color picture wallpaper hanging picture, very good!
Markdown笔记简明教程
go搭建服务器基础
Oracle 数据库表结构 Excel 导出
Random talk about Clickhouse join
To the Sultanate of Anderson
3Ds Max 精模obj模型导入ArcGIS Pro (二)要点补充
文本生成模型退化怎麼辦?SimCTG 告訴你答案
Phoenix architecture: an architect's perspective
oprator-1初识oprator
A small step in code change and a big leap in thinking
Markdown notes concise tutorial
Use the log server to output the topn of various Apache logs
It is urgent for enterprises to protect API security
Open source internship experience sharing: openeuler software package reinforcement test








