【问题标题】:Passing the data from the Express API Parameter to Request Module in MEAN Stack将 Express API 参数中的数据传递到 MEAN 堆栈中的请求模块
【发布时间】:2018-07-16 18:11:55
【问题描述】:

我在将参数从 Express API 传递到请求模块 URL 时遇到以下问题。

在下面的代码中假设我的请求详细信息为

request_data.url = http://localhost:3000/interface/en/

当用户输入网址为http://localhost:3000/interface/en/123456

我想将 123456 发送到线路

url: request_data.url + acct,

因此我的请求模块的最终 url 变为 http://localhost:3000/interface/en/123456

但是我下面的代码不起作用,有人可以在这里帮助我或建议我需要什么更改

代码

app.get('/interface/:env/:acct', (req, res) => {
    var acct = req.params.acct;
    var env = req.params.env;
    var hsResponse = request({
        proxy: proxyUrl,
        url: request_data.url + acct,
        headers: request_data.headers,
        method: request_data.method,
        form: oauth.authorize(request_data)
    }, function (error, response, body) {
        res.setHeader('Content-Type', 'application/json');
        res.send(body); //<-- send hsResponse response body back to your API consumer
    });
});

【问题讨论】:

  • 您需要在请求之外更新您的网址

标签: node.js express mean-stack meanjs


【解决方案1】:

请使用以下代码,

              app.get('/interface/:env/:acct', (req, res) => {
                  var acct = req.params.acct;
                  var env = req.params.env;

                  // here you need to update your url
                  request_data.url = request_data.url + acct;

                  var hsResponse = request({
                      proxy: proxyUrl,
                      url: request_data.url ,
                      headers: request_data.headers,
                      method: request_data.method,
                      form: oauth.authorize(request_data)
                  }, function (error, response, body) {
                      res.setHeader('Content-Type', 'application/json');
                      res.send(body); //<-- send hsResponse response body back to your API consumer
                  });
              });

我认为您正在使用OAuth,您将表单字段传递给request,这将需要使用现有映射的request_data 进行授权,例如URL 和其他attributes

希望对你有帮助!!

【讨论】:

    猜你喜欢
    • 2020-10-31
    • 1970-01-01
    • 2017-09-17
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 2014-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多