首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从react获取请求以表示不工作

从react获取请求以表示不工作
EN

Stack Overflow用户
提问于 2019-05-22 16:34:15
回答 1查看 1.1K关注 0票数 0

从react到express的提取路由显示了一个404未找到的错误。

这是客户端的代码

代码语言:javascript
复制
 deleteRoom(roomId){
    console.log('this is the current user in delete room', this.state.currentUser);
    fetch('http://localhost:3001/deleteRoom', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({ roomId }),
    })
      .then(response => {
        console.log('here is the resposne', response);
    }).catch(error => console.log('this is the delete room error', error))
      this.getRooms()
}

以下是服务器上的代码

代码语言:javascript
复制
const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const Chatkit = require('@pusher/chatkit-server')

const app = express()
app.use(function (req, res, next) {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Headers', 'Content-Type,Authorization');
  res.header('Access-Control-Allow-Methods', 'GET,POST,PUT,PATCH,DELETE');
  if (req.method === 'OPTIONS') {
    return res.send(204);
  }
  next();
});

const chatkit = new Chatkit.default({
  instanceLocator: 'v1:us1:77eb2c45-a91a-4942-bde8-1e7273b4788b',
  key: '76774b1d-427f-475f-8c05-643cdc492ca9:G3imsKMytcWbYeYolnMJcDpvYanvCgG6lpvrYP1YSjc=',
})

app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use(cors())

app.post('/users', (req, res) => {
  const {username} = req.body;
  console.log('here is the username on the server', username);
  chatkit
    .createUser({
      id: username,
      name: username
    })
    .then(()=> res.sendStatus(201))
    .catch(err => {
      if(error.error === 'services/chatkit/user_already_exists'){
        res.sendStatus(200)
      } else {
        res.status(err.status).json(err);
      }
    })
})

app.post('/authenticate', (req, res) => {
  const authData = chatkit.authenticate({userId: req.query.user_id})
  res.status(authData.status).send(authData.body);
})

app.post('/deleteRoom', (req, res) => {
  console.log('this is gettign to the server');
  chatkit.deleteRoom({
  id: req.body.roomId
   });
});

const PORT = 3001
app.listen(PORT, err => {
  if (err) {
    console.error(err)
  } else {
    console.log(`Running on port ${PORT}`)
  }
})

下面是来自客户机的响应消息:下面是响应: response {type:"cors",url:"http://localhost:3001/deleteRoom",重定向: false,状态: 404,ok: false,…}

渔获物不会开火

EN

回答 1

Stack Overflow用户

发布于 2019-05-22 18:16:25

是否只有/deleteRoom端点是错误的,或者任何post方法无法捕获?检查问题出在哪里

  1. 使用邮递员或失眠测试您的后端服务。工具将显示状态代码。
  2. 可以使用axios作为客户端获取的替代方法。

我对捡东西不太了解。当状态代码大于200时,Axios将自动抛出捕捉

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56261396

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档