如何将POST请求中的输入参数传递给GET请求使用?
解决GET请求使用POST参数的问题
你的核心问题是当前GET请求只能返回之前POST请求存在全局变量getVal里的数据,不仅会被多用户请求互相覆盖,还没法主动根据参数获取结果。下面给你三种可行的解决思路:
方案一:让GET请求直接接收查询参数(最推荐)
既然GET请求需要type和subtype参数,直接让客户端在GET请求的URL里带上这两个查询参数就行,不用依赖之前的POST请求。这样每个GET请求都能独立获取对应的数据,还能避免全局变量的并发问题。
修改后的路由代码:
app.route("/") .get(async (req,res)=>{ // 从查询参数里获取type和subtype const { type, subtype } = req.query; // 检查参数是否齐全 if(!type || !subtype){ return res.status(400).send("缺少type或subtype参数"); } try{ const getVal = await searchBar(type, subtype); res.send(getVal); }catch(err){ res.status(500).send("获取数据失败:" + err.message); } }) .post(async (req,res)=>{ const type = req.body.type; const subtype = req.body.subtype; try{ const getVal = await searchBar(type,subtype); res.send(getVal); }catch(err){ res.status(500).send("获取数据失败:" + err.message); } })
另外可以优化下getUsers函数,用filter代替map更简洁,同时处理空列表的情况:
const getUsers = async (type,subtype) =>{ const items = await getDocs(type); // 用filter筛选符合条件的项 const tempList = items.docs.map(d => d.data()).filter(item => item.subtype === subtype); // 处理空列表的情况,避免getRndInteger报错 if(tempList.length === 0){ return null; // 或者返回你需要的默认值 } return tempList[getRndInteger(0,tempList.length)]; };
方案二:用会话(Session)存储用户的参数
如果业务逻辑必须要求先调用POST传入参数,之后的GET才能用这些参数,那可以用会话来存储每个用户的type和subtype,避免全局变量的冲突。
步骤:
- 安装
express-session:npm install express-session - 配置会话中间件:
const session = require('express-session'); app.use(session({ secret: 'your-secret-key', // 换成自己的密钥 resave: false, saveUninitialized: false, cookie: { secure: false } // 生产环境建议设为true,配合HTTPS }));
- 修改路由代码:
app.route("/") .get(async (req,res)=>{ // 从session里取之前POST存的参数 const { type, subtype } = req.session; if(!type || !subtype){ return res.status(400).send("请先通过POST请求传入type和subtype参数"); } try{ const getVal = await searchBar(type, subtype); res.send(getVal); }catch(err){ res.status(500).send("获取数据失败:" + err.message); } }) .post(async (req,res)=>{ const type = req.body.type; const subtype = req.body.subtype; if(!type || !subtype){ return res.status(400).send("缺少type或subtype参数"); } // 把参数存在当前用户的session里 req.session.type = type; req.session.subtype = subtype; try{ const getVal = await searchBar(type,subtype); res.send(getVal); }catch(err){ res.status(500).send("获取数据失败:" + err.message); } })
方案三:用Cookie存储参数
和会话类似,可以把参数存在Cookie里,客户端后续的GET请求会自动带上Cookie,服务端读取后使用。
步骤:
- 安装
cookie-parser:npm install cookie-parser - 配置中间件:
const cookieParser = require('cookie-parser'); app.use(cookieParser());
- 修改路由代码:
app.route("/") .get(async (req,res)=>{ // 从Cookie里取参数 const { type, subtype } = req.cookies; if(!type || !subtype){ return res.status(400).send("请先通过POST请求传入type和subtype参数"); } try{ const getVal = await searchBar(type, subtype); res.send(getVal); }catch(err){ res.status(500).send("获取数据失败:" + err.message); } }) .post(async (req,res)=>{ const type = req.body.type; const subtype = req.body.subtype; if(!type || !subtype){ return res.status(400).send("缺少type或subtype参数"); } // 把参数存在Cookie里,设置过期时间(可选) res.cookie('type', type, { maxAge: 900000 }); // 15分钟过期 res.cookie('subtype', subtype, { maxAge: 900000 }); try{ const getVal = await searchBar(type,subtype); res.send(getVal); }catch(err){ res.status(500).send("获取数据失败:" + err.message); } })
内容的提问来源于stack exchange,提问作者Shekhar lohach
相关产品推荐
相关产品推荐

