Мой маршрут выборки от реакции на экспресс показывает ошибку 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}`)
}
})
Вот ответное сообщение от клиента: вот ответ: Response {тип: "cors", url: "http://localhost:3001/deleteRoom", перенаправлено: false, статус: 404, ok: false,…}
улов не срабатывает