我已经用 Express 提取了用户名、密码和另一个密码,如下所示:var urlencodedParser = bodyparser.urlencoded({extended: false});app.post('/', urlencodedParser, function(req, res) { console.log(req.body); var un = req.body.username; var pw1 = req.body.pwd1; var pw2 = req.body.pwd2;});然后我想用我创建的新变量做一些事情。我如何在此功能之外使用它们?
1 回答
喵喔喔
TA贡献1735条经验 获得超5个赞
您可以将它们设置为全局或将它们传递给下一个函数
var un, pw1, pw2
var urlencodedParser = bodyparser.urlencoded({extended: false});
app.post('/', urlencodedParser, function(req, res) {
console.log(req.body);
un = req.body.username;
pw1 = req.body.pwd1;
pw2 = req.body.pwd2;
});
或者传递它们:
var urlencodedParser = bodyparser.urlencoded({extended: false});
app.post('/', urlencodedParser, function(req, res) {
console.log(req.body);
doSomething(req.body.username, req.body.pw1, req.body.pw2);
});
function doSomething(un, pw1, pw2) {
...
}
添加回答
举报
0/150
提交
取消