Express.js服务器:使用中间件的PUT请求

问题描述 投票:0回答:1

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

对于每个请求,我提前准备一个const url = BASE_URL,用于实际的外部服务器请求。

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

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个回答
0
投票

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

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.