当前位置:网站首页>express get/post/delete...请求
express get/post/delete...请求
2022-07-27 16:20:00 【敢问】
GET 请求
GET请求是利用NodeJS的内置url模块去处理的,使用url.parse()方法
文件目录 在06文件夹下进行

所有的请求都在 06文件夹下的 index.html 发送 ,进行数据交互

index.html文件中 点击按钮发送 get 请求
<body>
<h2>请求内容</h2>
<p>
<input type="button" id="btn1" value="express 发送get请求">
</p>
<p>
<input type="button" id="btn2" value="express 发送post请求">
</p>
<p>
<input type="button" id="btn3" value="express 发送delete请求">
</p>
<script src="./js/jquery.min.js"></script>
<script>
$("#btn1").click(function () {
$.get("/get_data", {
"id": 10001,
"name": "小明"
}, function (data) {
console.log(data)
})
})
</script>
</body>app.js
var express = require("express");
var app = express();
//引入URL内置模块
var url = require("url");
//静态化文件
app.use(express.static("public"))
app.get("/get_data",function (req,res) {
let getData = url.parse(req.url,true).query;
if(getData.id == "10001"){
res.json({
"name":"小明",
"sex":"男",
"age":"25岁",
"score":"100"
})
}
console.log(getData)
})
app.listen(3000);说明:
- 引入URL内置模块进行处理 req.url 参数
url.parse()方法是用来处理url请求参数的
url.parse()方法的第一个参数req.url 当前URL地址的参数
url.parse()方法的第一个参数 true 表示的是对url的解析结果,如果为true解析结果的query就是一个JSON数据,否则就是url的字符串参数
![]()

POST请求
POST请求是利用npm 的formidable模块去处理的,通过IncomingForm()方法进行实例化产生表单处理工具的实例
POST请求参数是携带在上行报文上的,所以我们不能使用url模块再去处理了
借助npm模块帮我们处理
安装依赖:npm install --save formidable
.html发送请求
$("#btn2").click(function () {
$.post("/sub_data", {
"id": "10001",
"name": "小白",
"age": "18岁"
}, function (data) {
if (data.result == "success") {
alert("提交成功")
}
})
})app.js 处理请求
var express = require("express");
var app = express();
var formidable = require("formidable")
//页面静态化
app.use(express.static("public"));
app.post("/sub_data",function (req,res) {
// formidable对象内部有个IncomingForm方法,一旦被new调用后会返回表单处理工具的实例
var form = new formidable.IncomingForm();
// err是错误信息,fields表示的是数据源,files表示的是文件源
form.parse(req,function (err,fields,files) {
console.log(fields);
res.json({"result":"success"})
})
})
app.listen(3000)fields 的打印结果


其他方式请求 delete
express处理请求一共有二十多种,除了常见的GET和POST请求之外,还有其他的请求需要处理参数,其实除了GET请求外,所有的请求处理信息都可以用formidable去处理,方法和POST请求也是一样的
.html发送delete请求
$("#btn3").click(function () {
$.ajax({
"url": "/del_data",
"type": "delete",
"data": {
"id": "10001"
},
"success": function (data) {
alert(data)
},
"error": function () {
}
})
})app.js处理请求
app.delete("/del_data",function (req,res) {
// formidable对象内部有个IncomingForm方法,一旦被new调用后会返回表单处理工具的实例
var form = new formidable.IncomingForm();
// err是错误信息,fields表示的是数据源,files表示的是文件源
form.parse(req,function (err,fields,files) {
console.log(fields);
res.send(`学号为${fields.id}的学生删除成功`)
})
})
边栏推荐
- 你有没有在MySQL的order by上栽过跟头
- What does the number of network request interface layers (2/3 layers) mean
- Netred RGB mirror light touch chip-dlt8s15b-jericho
- Openstack login dashboard prompt authentication error
- 内网的公司邮箱服务器怎么发外部邮件
- Complete set of machine learning classification task effect evaluation indicators (including ROC and AUC)
- Join query and subquery
- Aircraft battle with enemy aircraft
- RuntimeError: output with shape [1, 256, 256] doesn‘t match the broadcast shape [3, 256, 256]【报错】
- Interviewer: what do you think is your biggest weakness?
猜你喜欢

Order timeout cancellation and commodity query by category

js实现简易表单验证与全选功能

浅谈JVM(面试常考)

低噪负离子风扇触摸IC

What does the number of network request interface layers (2/3 layers) mean

Talking about JVM (frequent interview)

Knowledge map - Jieba, pyhanlp, smoothnlp tools to realize Chinese word segmentation (part of speech)

I was forced to optimize the API gateway query interface

Quick access to website media resources

Commodity comment information and comment information classification
随机推荐
Complete set of machine learning classification task effect evaluation indicators (including ROC and AUC)
Using Jieba and pyhanlp tools to extract keyword words and sentences
Valueerror: found input variables with inconsistent numbers of samples: [80019456, 26673152] [error reporting]
Runtimeerror: output with shape [1, 256, 256] doesn't match the broadcast shape [3, 256, 256] [error]
Hbuilder submission code
Led learning eye protection table lamp touch chip-dlt8t10s-jericho
Multifunctional wireless remote control moxibustion instrument chip dltap703sd
飞机大战敌机出场
Typeerror: conv2d(): argument 'padding' (position 5) must be multiple of ints, not STR [error]
Interceptor拦截器
Conflict between blur event and click event in input box
商品评论信息与评论信息分类
INSUFFICIENT_ ACCESS_ ON_ CROSS_ REFERENCE_ ENTITY APEX / SALESFORCE
Music rhythm colorful gradient lamp chip -- dlt8s04a- Jericho
Filebeat.yml configuration file about the configuration of multiple services
飞机大战碰撞检测
Wechat payment and payment callback
建木持续集成平台v2.5.2发布
Commodity comment information and comment information classification
Login page tablelayout