当前位置:网站首页>Express接收请求参数
Express接收请求参数
2022-07-27 05:23:00 【THER1881】
一、Express接收请求参数
1、接收get请求参数:使用req.query接收
req.query.参数名
演示;
1.1、在WebStrom中创建express项目:


1.2、创建完成后,运行服务器,检查是否在3000端口上成功运行:
1.3、创建路由,命名为gets.js:
const express = require('express')
const router = express.Router()
//创建get请求的处理代码
http://localhost:3000/params/gets
router.get('/gets',(req, res) => {
//通过req.query接收get请求的参数
res.send(req.query)
console.log(req.query)
})
module.exports = router
1.4、配置app.js文件:
var getparamsRouter=require('./routes/getparams');
app.use('/params',getparamsRouter);
需要注意的是在配置app.js时,导入自定义路由文件时必须放在
var app = express()前,否则可能会出现错误。
图示:
1.5、在Apipost中进行调试


2、接收post请求参数:使用req.body接收
req.body.参数名
post请求前期的创建express项目等步骤和get请求一致,这里就不在做演示。
2.1、创建post.js
演示:
const express = require('express')
const router = express.Router()
//创建post请求的处理代码
router.post('/posts',(req, res) => {
res.send(req.body)
console.log("姓名"+req.body.username)
console.log("密码"+req.body.pwd)
})
module.exports = router
依旧先在Apipost中调试
2.2在客户端中进行验证:
首先创建HTML页面:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
div{
width: 300px;
margin: 100px 600px 500px 400px;
}
</style>
<body>
<div>
<form action="http://localhost:3000/params/gets" method="get">
<label for="">
用户名:
<input type="text" name="username">
</label>
<br><br>
<label for="">
密 码:
<input type="password" name="pwd" id="">
</label>
<br><br>
<button type="submit">提交</button>
</form>
</div>
</body>
</html>

点击提交后:

3、接收路由参数
定义路由时,在请求路径中传递的参数例如请求路径“/find/:id”中的“:id”是一个参数占位符,当浏览器向“/find/:id”地址发送请求时,“:id”对应的值就是参数值。在接收参数时,使用req.params
演示:
3.1、gets.js:
const express = require('express')
const router = express.Router()
router.get('/find/:id/:addr',(req, res) => {
res.send(req.params)
console.log('路由参数:'+req.params.id)
console.log('路由参数:'+req.params.addr)
})
module.exports = router
3.2、进行验证:

边栏推荐
- FTP服务器的搭建
- GoLand 编写go程序
- Relevant preparation materials for system design
- FTX US推出FTX Stocks,向主流金融行业迈进
- Shell script backup MySQL database
- 面试常问的问题总结【呕心沥血熬了一个晚上总结的】
- Recommended by the world's most technologically competent smart contract security audit company in 2022
- Publish a building segmentation database with a resolution of 0.22m
- FTX.US推出股票和ETF交易服务,让交易更透明
- logging日志的封装
猜你喜欢

Multimodal database | star ring technology multimode database argodb "one database for multiple purposes", building a high-performance Lake warehouse integrated platform

Many of the world's top 500 enterprises gathered at the second digital Expo, and the digital industry curtain is about to open!

Soul 递交港股上市申请,加快社交聚集地多元化、场景化的布局

2022上半年英特尔有哪些“硬核创新”?看这张图就知道了!

Soul submitted an application for listing in Hong Kong stocks, accelerating the diversified and scene based layout of social gathering places

Stress performance test of website based on AB tool under Apache

Linux安装与卸载MySql

关于在Gazebo中给无人机添加相机(摄像头)之后,无人机无法起飞

关于testng相关标签的使用

面试常问的问题总结【呕心沥血熬了一个晚上总结的】
随机推荐
Relevant preparation materials for system design
win10 添加虚拟网卡,配置op路由
How to write yaml file in a standard way
Concept and principle of DHCP
shell函数
numpy数组和图片互转
向日葵教大家如何防范拒绝服务攻击漏洞?
Packaging of logging logs
如何避免漏洞?向日葵远程为你讲解不同场景下的安全使用方法
关于在Gazebo中给无人机添加相机(摄像头)之后,无人机无法起飞
使用密钥方式登录阿里云服务器
云原生运行环境搭建
Redis快速学习
FTP服务器的搭建
Shell脚本一键配置LAMP
GoLand 编写go程序
Publish a building segmentation database with a resolution of 0.22m
ESXI虚拟机启动,模块“MonitorLoop”打开电源失败
shell的编程规范and重定向与管道操作
ArcGIS for JS API (1) get all field names of FeatureLayer