【问题标题】:Express.js Server: PUT Request using MiddlewareExpress.js 服务器:使用中间件的 PUT 请求
【发布时间】:2018-09-11 17:29:24
【问题描述】:

我有一个可用的 Node/Express 服务器,我正在使用它通过 localhost 向外部 API 发出请求。正如您在我的示例代码中看到的,我使用node-fetch 处理我的基本 GET 请求。

对于每个请求,我都会提前准备一个const url = BASE_URL,这是实际外部服务器请求所需要的。

但我无法使用PUT-Request,因为我无法使用node-fetch。那么我该怎么做才能用 PUT-Request 的实际 URL 通知我的 Express 服务器?

PUT-Request 直到这里才起作用。

/* Route: Get Appointment it's availability times */
app.get('/availability/times/:id/:date/:locationId', (req, res) => {
  var id = req.params.id;
  var date = req.params.date;
  var locationId = req.params.locationId;
  const url = BASE_URL + '/availability/times?appointmentTypeID=' + id + '&date=' + date + '&calendarID=' + locationId;;
  fetch(url, {
      mode: "no-cors",
      method: "GET",
      headers: {
        'Content-Type': 'application/json',
        'X-Requested-With': 'content-type'
      },
    })
    .then(response => response.json())
    .then(json => {
      res.json(json);
    });
});

app.put('/appointments/:id/cancel', (req, res) => {
  var id = req.params.id;
  const url = BASE_URL + '/appointments/' + id + '/cancel';
  res.send(req.body)
});

【问题讨论】:

    标签: javascript node.js express server put


    【解决方案1】:

    如果您说在您的 put 请求中未定义 fetch,请确保在任何路由 var fetch = require('node-fetch') 之前将其置于顶部。对于您的基本网址,您应该将其存储在配置文件中。创建一个名为 config.js 的文件,如下所示:

    module.exports = {
      'BASE_URL': 'yoururlhere'
    }
    

    然后在您的快速服务器var config = require('pathToConfig'); 中使用它,您可以通过指定config.BASE_URL 来使用它

    如果这没有帮助,请更具体地说明您的实际问题是什么

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-03
      • 1970-01-01
      • 2017-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多