Не удается отправить на локальный хост "POST http://localhost: 8000 / sendpage 404 (Not Found)" в консоли - PullRequest
0 голосов
/ 26 апреля 2020

Я не могу публиковать что-либо на моем локальном / sendpage, который является json файлом. Хотя я могу получить от этого. Почему? Ошибка консоли: «POST http://localhost: 8000 / sendpage 404 (Not Found)»

СЕРВЕР:

var express = require('express');
var app = express();
var http = require('http').Server(app);
var path = require('path');

var port=8000;
app.set('port',(process.env.PORT || port));

// Serve static assets from public/
app.use(express.static(path.join(__dirname, 'public/')));

var server = http.listen(app.get('port'), function () {
  console.log('Server listening on port ' + app.get('port'));
});


app.get('/', function (req, res) {
  res.sendFile(path.join(__dirname, 'public/index.html'));
});

app.get('/sendpage', function(req,res){
  res.json({
"id":40000,
"body":"Name: Vera\nEmail: vera.wid@gmail.com\n",
"read":true,"secret":false,
"direction":"in",
"readAt":"2020-04-24T13:45:41Z",
"createdAt":"2020-04-24T13:45:13Z",
"updatedAt":"2020-04-24T13:45:41Z",
"ChatWebsiteId":1,
"ChatInteractionId":249,
"UserId":11,
"ContactId":11584,
"AttachmentId":null,
"User":{"transport":null,"nat":null,"allow":null,"insecure":null,"permissions":[],"phoneBarEnableVideoRecording":false,"id":11,"fullname":"Bruce Wayne","alias":null}
})



});

Функция javascript, которая является подключен к кнопке:

function addMessage() {

  axios.post('http://localhost:8000/sendpage', {
  "id":40000,
  "body":"Name: Vera\nEmail: vera.wid@gmail.com\n",
  "read":true,"secret":false,
  "direction":"in",
  "readAt":"2020-04-24T13:45:41Z",
  "createdAt":"2020-04-24T13:45:13Z",
  "updatedAt":"2020-04-24T13:45:41Z",
  "ChatWebsiteId":1,
  "ChatInteractionId":249,
  "UserId":11,
  "ContactId":11584,
  "AttachmentId":null,
  "User":{"transport":null,"nat":null,"allow":null,"insecure":null,"permissions":[],"phoneBarEnableVideoRecording":false,"id":11,"fullname":"Bruce Wayne","alias":null}



})




.then(res=>console.log(res))
.catch(err=>console.error(err))
}

Почему, когда я заменяю POST на get, он работает?

1 Ответ

2 голосов
/ 26 апреля 2020

Вы не обрабатываете запрос POST в своем express коде, как вы делаете с GET

app.get('/sendpage', function(req,res){
  res.json({
  "id":40000,
  "body":"Name: Vera\nEmail: vera.wid@gmail.com\n",
  "read":true,"secret":false,
  "direction":"in",
  "readAt":"2020-04-24T13:45:41Z",
  "createdAt":"2020-04-24T13:45:13Z",
  "updatedAt":"2020-04-24T13:45:41Z",
  "ChatWebsiteId":1,
  "ChatInteractionId":249,
  "UserId":11,
  "ContactId":11584,
  "AttachmentId":null,
  "User":{"transport":null,"nat":null,"allow":null,"insecure":null,"permissions":[],"phoneBarEnableVideoRecording":false,"id":11,"fullname":"Bruce Wayne","alias":null}
})

Вы можете добавить его следующим образом

app.post('/sendpage',function(req,res){
  //do something
})
...