当前位置:网站首页>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')
})
边栏推荐
- 对多态的理解
- Fletter nested hell? No, constraintlayout to save!
- The 16th Heilongjiang Provincial Collegiate Programming Contest
- 网络营销之四大误解
- 文本生成模型退化怎麼辦?SimCTG 告訴你答案
- Apply for vector bus protocol color picture wallpaper hanging picture, very good!
- Three techniques for reducing debugging time of embedded software
- Learning summary
- Radar data processing technology
- Clickhouse Native Monitoring item, System table Description
猜你喜欢

Introduction of 3D Max fine model obj model into ArcGIS pro (II) key points supplement

Apply for vector bus protocol color picture wallpaper hanging picture, very good!

Double solid histogram / double y-axis

《ClickHouse原理解析与应用实践》读书笔记(1)

时空数据挖掘:综述

jenkins下载插件下载不了,解决办法

qsort函数和模拟实现qsort函数

MySQL高级篇3

DM8:生成DM AWR报告

布隆过滤器
随机推荐
测试勋章1234
clickhouse原生監控項,系統錶描述
Clickhouse distributed table engine
What happens when word encounters an error while trying to open a file?
Ten security measures against unauthorized access attacks
电子方案开发——智能跳绳方案
减少嵌入式软件调试时间的三个技巧
ceshi deces
科研中遇到迷茫困惑如何向前一步?如何在科研中发挥女性优势?
笔记【JUC包以及Future介绍】
文本生成模型退化怎么办?SimCTG 告诉你答案
Open source internship experience sharing: openeuler software package reinforcement test
本地浏览器打开远程服务器上的Jupyter Notebook/Lab以及常见问题&设置
CA I ah, how many times Oh, ah sentence IU home Oh 11111
Oracle 数据库表结构 Excel 导出
Coefficient of variation method matlab code [easy to understand]
的撒啊苏丹看老司机
Adobe-Photoshop(PS)-脚本开发-去除文件臃肿脚本
To the Sultanate of Anderson
雷达数据处理技术