'How to send axios post to socket io server?
I need to send a POST to a socket.io server and print it on the screen. This needs to be done in real time. I will open up socket io server, leave it open on my browser, and every time I send a post to it, it needs to print the value on the screen. I have no prior experience in Node,so I'm a little bit lost on how to do it. This is my socket.io server (almost the exact same one from the documentation tutorial):
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
io.on('connection', (socket) => {
console.log("USER CONNECTED!")
});
server.listen(3000, () => {
console.log('listening on *:3000');
});
And I'm sending a post to it from another server every time it gets a request. I'm using axios:
axios.post('http://myhost:3000/',qs.stringify({
test: 'test'
}))
.then(res => {
console.log(` >STATUS: ${res.status}`);
})
.catch(error => {
console.error(error);
})
This is the error I'm getting:
Error: Request failed with status code 404
at createError (/var/www/html/MYFOLDER/node_modules/axios/lib/core/createError.js:16:15)
at settle (/var/www/html/MYFOLDER/node_modules/axios/lib/core/settle.js:17:12)
at IncomingMessage.handleStreamEnd (/var/www/html/ROMA/node_modules/axios/lib/adapters/http.js:269:11)
at IncomingMessage.emit (node:events:402:35)
at endReadableNT (node:internal/streams/readable:1340:12)
at processTicksAndRejections (node:internal/process/task_queues:83:21)
I don't know exactly how to do what I described. Any other suggestion is accepted,not only fixes on my code.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
