【前端知识】Node——http模块url模块的常用操作

news/2025/2/12 16:48:27/

一、创建简易Server

const http = require('http');
const URL = require('url');const HTTP_PORT = 8088;const server = http.createServer((req, res) => {// req:request请求对象,包含请求相关的信息;// res:response响应对象,包含我们要发送给客户端的信息;const { headers, method, url } = req;console.log( headers, method, url);
});server.listen(HTTP_PORT, () => {console.log(`🚀 Outman 服务器已启动,端口:${HTTP_PORT}`);
})

二、url相关处理

const http = require('http');
const URL = require('url');const HTTP_PORT = 8088;const server = http.createServer((req, res) => {// req:request请求对象,包含请求相关的信息;// res:response响应对象,包含我们要发送给客户端的信息;const { headers, method, url } = req;console.log( headers, method, url);// url处理if(url === '/login'){res.end('hello outman');}else if(url === '/products'){res.end('products list');}else{res.end('error request');}// url带参解析(GET)const parseInfo = URL.parse(req.url);console.log(parseInfo);const { pathname, query } = URL.parse(req.url);const queryObj = URL.parse(query);console.log(pathname, queryObj);
});server.listen(HTTP_PORT, () => {console.log(`🚀 Outman 服务器已启动,端口:${HTTP_PORT}`);
})

三、请求配置与监听

const http = require('http');
const URL = require('url');const HTTP_PORT = 8088;const server = http.createServer((req, res) => {// req:request请求对象,包含请求相关的信息;// res:response响应对象,包含我们要发送给客户端的信息;const { headers, method, url } = req;console.log( headers, method, url);// req 配置 & 监听req.setEncoding('utf-8');// 监听获取body中的参数(POST)req.on('data', (data) => {console.log('data', data);const { username, password } = JSON.parse(data);console.log(username, password);});req.on('end', () => {console.log('传输结束');});res.end('outman msg')
});server.listen(HTTP_PORT, () => {console.log(`🚀 Outman 服务器已启动,端口:${HTTP_PORT}`);
})

四、常用HTTP CODE

HTTP状态码状态描述信息说明
200OK请求成功
201CreatedPOST请求,创建新的资源
301Moved Pemanently请求资源的URL已经修改,响应中会给出新的URL
400Bad Request客户端的错误,服务器无法或者不进行处理
401Unauthorized未授权的错误,必须携带请求的身份信息
403Forbidden客户端没有权限访问,被拒接
404Not Found服务器找不到请求的资源
500Internal Server Error服务器遇到了不知道如何处理的情况
503Service Unavailable服务器不可用,可能处理维护或者重载状态,暂时无法访问

http://www.ppmy.cn/news/1232287.html

相关文章

2311rust过程宏的示例

原文 Rust2018中的过程宏 在Rust2018版本中,我最喜欢的功能是过程宏.在Rust中,过程宏有着悠久而传奇的历史(并继续拥有传奇的未来!) 因为2018年版极大改善了定义和使用它们的体验. 什么是过程宏 过程宏是,在编译时用一段语法,生成新语法的函数.Rust2018中的过程宏有三个风格…

【图像分类】【深度学习】【Pytorch版本】Inception-ResNet模型算法详解

【图像分类】【深度学习】【Pytorch版本】Inception-ResNet模型算法详解 文章目录 【图像分类】【深度学习】【Pytorch版本】Inception-ResNet模型算法详解前言Inception-ResNet讲解Inception-ResNet-V1Inception-ResNet-V2残差模块的缩放(Scaling of the Residuals)Inception-…

音视频项目—基于FFmpeg和SDL的音视频播放器解析(十七)

介绍 在本系列,我打算花大篇幅讲解我的 gitee 项目音视频播放器,在这个项目,您可以学到音视频解封装,解码,SDL渲染相关的知识。您对源代码感兴趣的话,请查看基于FFmpeg和SDL的音视频播放器 如果您不理解本…

二重积分一般计算步骤

文章目录 二重积分计算的一般步骤分析积分区域草图积分区域对称性区域分割 可以简化计算的两类情况👺利用对称性和奇偶性计算双轴对称变量的对称性轮换对称计算 选择合适的坐标系直角坐标系先 y y y后 x x x先 x x x后y 极坐标系 确定积分区间积分次序积分限 应用例…

es各种报错问题及解决方案20231121

报错一 org.elasticsearch.ElasticsearchStatusException: Elasticsearch exception [typesearch_phase_execution_exception, reasonall shards failed]Suppressed: org.elasticsearch.client.ResponseException: method [POST], host [http://localhost:9200], URI [/wzx-te…

CentOS上搭建SVN并自动同步至web目录

一、搭建svn环境并创建仓库: 1、安装Subversion: yum install svn2、创建版本库: //先建目录 cd /www mkdir wwwsvn cd wwwsvn //创建版本库 svnadmin create xiangmumingcheng二、创建用户组及用户: 1、 进入版本库中的配…

如何将Docker的构建时间减少40%

与许多公司类似,我们为产品中使用的所有组件构建docker映像。随着时间的推移,其中一些映像变得越来越大,我们的CI构建花费的时间也越来越长。我的目标是CI构建不超过5分钟——差不多是喝杯咖啡休息的理想时间。如果构建花费的时间超过这个时间…

解决解析PDF编码报错(以pdfminer为例):UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte xxx

解决方法 博主使用的是pdfminer解析PDF文档,这个解决方法是通用的,只需要使PDFParser传入的文件为二进制文件即可,示例程序: from pdfminer.pdfparser import PDFParserpdf_parser PDFParser(open("pdf文件.pdf", &q…