【问题标题】:How to pass parameter from express to normal const variable?如何将参数从 express 传递给普通的 const 变量?
【发布时间】:2020-08-21 13:00:44
【问题描述】:

我有一个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 中捕获这些参数?

【问题讨论】:

    标签: node.js express asynchronous parameters parameter-passing


    【解决方案1】:

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

    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...
    };
    

    【讨论】:

      猜你喜欢
      • 2020-07-04
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-20
      • 1970-01-01
      相关资源
      最近更新 更多