最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

我如何通过带有请求的Express传递请求API响应?

运维笔记admin9浏览0评论

我如何通过带有请求的Express传递请求API响应?

我如何通过带有请求的Express传递请求API响应?

我具有以下服务器端设置:

router.get(
    "/auth/google",
    passport.authenticate("google", { scope: ['Profile','.readonly'] })
);

router.get(
    "/auth/google/callback",
    passport.authenticate("google", { failureRedirect: "/error", session: false }),
    function(req, res) {
        var token = req.user.token;
        res.redirect("/getData?token=" + token);
    }
);

router.get('/getData', function(req, res) {
    var token = req.query.token;
request('=' + token,  
function (error, response, body) {
          let views = []
   JSON.parse(body).items.forEach(view => {
            views.push({
              name: view.webPropertyId + ' - ' + view.name + ' (' + view.websiteUrl + ')'
            })
          })
res.send(views)
});
})

带有以下客户端组件:

     componentDidMount() {
    fetch('http://localhost:5000/getData',
    {
          method: 'put',
          headers: {'Content-Type': 'application/json'}
      })
      .then(res => { 
        if (!res.ok) {
          throw res;
        }
        return res.json()
      }).then(data => {
        this.setState({loading: false, data});
      }).catch(err => {
        console.error(err);
        this.setState({loading: false, error: true});
      });
  }

我应该如何配置express,以便获取后端并通过前端的API请求传递响应?

目前,我收到以下错误Failed to load resource: the server responded with a status of 500 (Internal Server Error)

回答如下:

也许尝试在您的提取参数中将put方法切换为get方法-方法:'GET'

发布评论

评论列表(0)

  1. 暂无评论