作者:北斗七星 | 来源:互联网 | 2023-08-19 14:42
本文主要介绍关于前端,开发语言的知识点,对【【Express接收Get、Post、路由请求参数】】和【公司规定所有接口都用post请求】有兴趣的朋友可以看下由【꒰ঌsnail໒꒱】投稿的技术文章,希望
本文主要介绍关于前端,开发语言的知识点,对【【Express接收Get、Post、路由请求参数】】和【公司规定所有接口都用post请求】有兴趣的朋友可以看下由【꒰ঌsnail໒꒱】投稿的技术文章,希望该技术和经验能帮到你解决你所遇的node.js,express框架相关技术问题。
公司规定所有接口都用post请求
目录 一、Express接收请求参数1、接收get请求参数:使用req.query接收2、接收post请求参数:使用req.body接收3、接收路由参数:定义路由时,在请求路径中传递的参数
前言:
使用原生Node.js处理GET和POST请求参数是非常麻烦的,例如,为了获取GET请求参数,需要使用url模块对请求地址进行解析。为了降低开发的难度,Express通过req.query、req.body和第 三方模块body-parser对请求参数进行了处理。下面来了解Express如何接收请求参数。
回顾前面知识,在webstorm创建express项目(项目名称:express-params)
第一步:
![【Express接收Get、Post、路由请求参数】 公司规定所有接口都用post请求](https://img1.php1.cn/3cd4a/24d65/1c8/29411756d523b72a.png)
第二步:
![【Express接收Get、Post、路由请求参数】 公司规定所有接口都用post请求](https://img1.php1.cn/3cd4a/24d65/1c8/96a2d9a5b210c0f9.png)
第三步:
![【Express接收Get、Post、路由请求参数】 公司规定所有接口都用post请求](https://img1.php1.cn/3cd4a/24d65/1c8/391bb71fd4314749.png)
第四步:运行
![【Express接收Get、Post、路由请求参数】 公司规定所有接口都用post请求](https://img1.php1.cn/3cd4a/24d65/1c8/5662c0247b1cb2da.png)
![【Express接收Get、Post、路由请求参数】 公司规定所有接口都用post请求](https://img1.php1.cn/3cd4a/24d65/1c8/d34d4fa1928b4eec.png)
第五步:检查服务器是否已经在3000端口上成功运行:
![【Express接收Get、Post、路由请求参数】 公司规定所有接口都用post请求](https://img1.php1.cn/3cd4a/24d65/1c8/067db09a67e788e6.png)
出现上述界面,表明已成功创建。
一、Express接收请求参数 1、接收get请求参数:使用req.query接收
req.query.参数名
首先:创建路由
![【Express接收Get、Post、路由请求参数】 公司规定所有接口都用post请求](https://img1.php1.cn/3cd4a/24d65/1c8/fcd4666ae84be9ca.png)
命名为:getparams.js
var express =require('express')
var router=express.Router()
router.get('/gets',((req, res) =>{
res.send(req.query)
console.log('姓名:'+req.query.userName)
console.log('年龄:'+req.query.userPwd)
} ))
module.exports =router
其次:配置app.js
var getparamsRouter=require('./routes/getparams');
app.use('/params',getparamsRouter);
然后:在postman软件上进行调试
![【Express接收Get、Post、路由请求参数】 公司规定所有接口都用post请求](https://img1.php1.cn/3cd4a/24d65/1c8/29af558dea0728a2.png)
最后:在客户端浏览器上进行访问
![【Express接收Get、Post、路由请求参数】 公司规定所有接口都用post请求](https://img1.php1.cn/3cd4a/24d65/1c8/fec68498d8b07483.png)
2、接收post请求参数:使用req.body接收
req.body.参数名
getparams.js:
var express =require('express')
var router=express.Router()
router.post('/posts',(req, res) => {
res.send(req.body)
console.log('姓名:'+req.body.userName)
console.log('年龄:'+req.body.userPwd)
})
module.exports =router
在postman软件进行调试:
![【Express接收Get、Post、路由请求参数】 公司规定所有接口都用post请求](https://img1.php1.cn/3cd4a/24d65/1c8/0a523430203684a0.png)
出现下列信息,表名请求成功:
![【Express接收Get、Post、路由请求参数】 公司规定所有接口都用post请求](https://img1.php1.cn/3cd4a/24d65/1c8/b429ce8fa58b30e6.png)
在客户端进行请求验证:
创建一个登录页面login.html:
DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="token punctuation">"> <title>Document
title>
head> <style> div{
width: 300px; margin: 100px 600px 500px 400px; }
style> <body> <div> <form action="http://localhost:3000/params/posts" method="post"> <label> 用户名:<input type="text" name="userName">
label> <br><br> <label> 密 码:<input type="password" name="userPwd">
label> <br><br> <button type="submit">登录
button>
form>
div>
body>
html>
![【Express接收Get、Post、路由请求参数】 公司规定所有接口都用post请求](https://img1.php1.cn/3cd4a/24d65/1c8/4bd2d61d5b6a484e.png)
输入用户名,和密码,点击登录,跳转成功:
![【Express接收Get、Post、路由请求参数】 公司规定所有接口都用post请求](https://img1.php1.cn/3cd4a/24d65/1c8/1ea569d14ca19699.png)
3、接收路由参数:定义路由时,在请求路径中传递的参数
例如:请求路径“/find/:id”中的“:id”是一个参数占位符,
当浏览器向“/find/:id”地址发送请求时,“:id”对应的值就是参数值。
在接收参数时,使用req.params
getparams.js:
var express =require('express')
var 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
在postman软件进行调试:
![【Express接收Get、Post、路由请求参数】 公司规定所有接口都用post请求](https://img1.php1.cn/3cd4a/24d65/1c8/8adc607f7afdd3fd.png)
本文《【Express接收Get、Post、路由请求参数】》版权归꒰ঌsnail໒꒱所有,引用【Express接收Get、Post、路由请求参数】需遵循CC 4.0 BY-SA版权协议。