如何将参数从express传递到普通const变量?

问题描述

我有一个express POST路由器,它将数据发送到其他NodeJS脚本。

例如,我通过username,password

发送

这是我要将这些参数发送到的脚本

const options = {
  cookiesPath: './cookies.json',username: {I want to send username here},password: {I want to send password here},userslist: null,dryrun: false,}

稍后,该options文件在另一个async函数内的代码中再次被调用

const doWork = async (users = []) => {
    usersToBeUsed = users;
    const instauto = await Example(browser,options);
}

如何在const options中捕获这些参数?

解决方法

我想您要发送一个包含两个输入的表单,分别是用户名和密码及其值。您的路线如下所示:

router.post("/my/path",controller.myFunction);

然后您的函数应如下所示:

exports.myFunction = (req,res) => {
  console.log(req.body); //See how your data looks like
  const options = {
    cookiesPath: './cookies.json',username: req.body.username,password: req.body.password,userslist: null,dryrun: false,};
  //do something with the data and send the response,render,etc...
};