从反应中获取请求以表示无法正常工作

时间:2019-05-22 16:34:15

标签: reactjs express fetch pusher chatkit

我的从反应到表达的提取路线显示404未找到错误。

这是客户端上的代码

 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()
}

这是服务器上的代码

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}`)
  }
})

这是来自客户端的响应消息: 这是响应:响应{type:“ cors”,url:“ http://localhost:3001/deleteRoom”,重定向:false,状态:404,ok:false,...}

渔获物不发火

1 个答案:

答案 0 :(得分:0)

是仅 private void queryEmpresa(){ ParseQuery<ParseUser> query = ParseUser.getQuery(); query.whereEqualTo("objectId", ParseUser.getCurrentUser().getObjectId()); query.include("Empresa"); query.findInBackground(new FindCallback<ParseUser>() { @Override public void done(List<ParseUser> objects, ParseException e) { for (ParseUser obj:objects ) { empresa=obj.getParseObject("Empresa"); String id=empresa.getObjectId(); } } }); } 个端点错误还是任何post方法无法捕获? 检查问题出在哪里

  1. 使用邮差或失眠症来测试您的后端服务。工具将显示状态代码。

  2. 您可以使用axios lib作为客户端获取的替代方式

我对提取并不了解。当状态码超过/deleteRoom

时,Axios将自动抛出异常