The server does not recognize the JSON coming from the client. From this <input type="text" id="message"> field, I send the data via XMLHttpRequest :
var xhr = new XMLHttpRequest(); xhr.open("POST", "/publish", true); var data = JSON.stringify({message: message.value}); xhr.send(data); Here's what on the server (using Express ):
var express = require('express'); var app = express(); /* code */ var bodyParser = require('body-parser'); // Использую модуль body-parser app.use(bodyParser.json()); // для чтения request.body app.post("/publish", function(req, res){ var msg = req.body.message; chat.publish(msg); console.log(msg); res.send(msg); }); On the console.log client, it says that the data from input converted correctly. Those. if I enter " something ", then the string that will be sent to the server is {"message":"something"} . On the server, console.log says undefined . BUT. By sending data using a third-party query utility (Postman) of the same type {"message":"something"} , the server recognizes the message . What could be wrong?