当前位置:网站首页>Express get/post/delete... Request
Express get/post/delete... Request
2022-07-27 18:59:00 【venture to ask】
GET request
GET Request is to use NodeJS The built-in url Module to deal with , Use url.parse() Method
File directory stay 06 Folder

All requests are in 06 Under folder index.html send out , Data interaction

index.html In file Click the button to send get request
<body>
<h2> Request content </h2>
<p>
<input type="button" id="btn1" value="express send out get request ">
</p>
<p>
<input type="button" id="btn2" value="express send out post request ">
</p>
<p>
<input type="button" id="btn3" value="express send out delete request ">
</p>
<script src="./js/jquery.min.js"></script>
<script>
$("#btn1").click(function () {
$.get("/get_data", {
"id": 10001,
"name": " Xiao Ming "
}, function (data) {
console.log(data)
})
})
</script>
</body>app.js
var express = require("express");
var app = express();
// introduce URL Built-in module
var url = require("url");
// Static file
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":" Xiao Ming ",
"sex":" male ",
"age":"25 year ",
"score":"100"
})
}
console.log(getData)
})
app.listen(3000);explain :
- introduce URL Built in module for processing req.url Parameters
url.parse() The method is used to deal with url Request parameter
url.parse() The first parameter of the method req.url At present URL The parameters of the address
url.parse() The first parameter of the method true It means right url The resolution result of , If true Of analytical results query It's just one. JSON data , Otherwise, it would be url String parameter of
![]()

POST request
POST Request is to use npm Of formidable Module to deal with , adopt IncomingForm() Method to generate an instance of the form processing tool
POST The request parameters are carried on the uplink message , So we can't use url The module will deal with it again
With the help of npm Modules help us deal with
Installation dependency :npm install --save formidable
.html Send a request
$("#btn2").click(function () {
$.post("/sub_data", {
"id": "10001",
"name": " The small white ",
"age": "18 year "
}, function (data) {
if (data.result == "success") {
alert(" Submit successfully ")
}
})
})app.js Processing requests
var express = require("express");
var app = express();
var formidable = require("formidable")
// Page static
app.use(express.static("public"));
app.post("/sub_data",function (req,res) {
// formidable There is one inside the object IncomingForm Method , Once being new After calling, an instance of the form processing tool will be returned
var form = new formidable.IncomingForm();
// err It's the wrong message ,fields Represents the data source ,files Represents the file source
form.parse(req,function (err,fields,files) {
console.log(fields);
res.json({"result":"success"})
})
})
app.listen(3000)fields Print result of


Request in other ways delete
express There are more than 20 kinds of requests , In addition to the usual GET and POST Besides the request , There are other requests that need to process parameters , Except GET Out of request , All request processing information can be used formidable To deal with , Methods and POST The same is true of requests
.html send out delete request
$("#btn3").click(function () {
$.ajax({
"url": "/del_data",
"type": "delete",
"data": {
"id": "10001"
},
"success": function (data) {
alert(data)
},
"error": function () {
}
})
})app.js Processing requests
app.delete("/del_data",function (req,res) {
// formidable There is one inside the object IncomingForm Method , Once being new After calling, an instance of the form processing tool will be returned
var form = new formidable.IncomingForm();
// err It's the wrong message ,fields Represents the data source ,files Represents the file source
form.parse(req,function (err,fields,files) {
console.log(fields);
res.send(` The student number is ${fields.id} Students deleted successfully `)
})
})
边栏推荐
猜你喜欢

Led learning eye protection table lamp touch chip-dlt8t10s-jericho

建木持续集成平台v2.5.2发布

Kinect for Unity3d----KinectManager

The understanding of string in C.

Uni app form submit button send request

你想得到想不到的MySQL面试题都在这了(2022最新版)

百度地图鹰眼轨迹服务

LED带风扇护眼学习台灯触摸芯片-DLT8S12A

MySQL 01 关系型数据库设计

Use mobaxtermto establish a two-tier springboard connection
随机推荐
个人中心--订单业务流程
Latex使用--subfigure竖排图形
express
MySQL 03 高级查询(一)
Commodity comment information and comment information classification
Leetcode brushes questions the next day
连接查询和子查询
C static method and non static method
我人都傻了,CompletableFuture和OpenFegin一起使用竟然报错
idea 2020.1社区版下载体验
商品评论信息与评论信息分类
Interviewer: what do you think is your biggest weakness?
JS to realize simple form verification and select all functions
JS中的数组与对象
面试官:你觉得你最大的缺点是什么?
normal distribution, lognormal distribution,正态随机数的生成
MySQL 05 stored procedure
Infrared hyperspectral survey
`this.$ Emit ` the child component passes multiple parameters to the parent component
Some advice for NS2 beginner.