error conexion vue-socket ERR_CONNECTION_REFUSED - node.js

I have not managed to connect my vue js to my nodejs server. The configuration is the following, server side:
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);
app.use(express.static(__dirname + '/dist'));
app.set('port', process.env.PORT || 5050);
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname + '/dist/index.html'));
});
server.listen(app.get('port'), () => console.log(`http://localhost:${app.get('port')}`));
on the client side:
Vue.use(new VueSocketio({
debug: true,
connection: 'http://localhost:5050'
}));
when I test in local everything works perfect, the socket opens the events are issued without failure, the problem lies when I upload a server, the constant error:
vue-socketio.js:1 GET http://localhost:5050/socket.io/?EIO=3&transport=polling&t=MUu_7m4 net::ERR_CONNECTION_REFUSED
try to put the ip of the server provider and still the problem, I have seen many issues regarding this but I can not get a clear solution, I appreciate the help they can give me, thanks!

Related

Full Stack: Http failure response for url: 0 Unknown Error

I am trying to deploy my full stack application built using MySQL, Express, Angular and Node on Plesk Obsidian. However, I am facing an issue during the deployment. When my client side sends a request to the server then, I get the following error:
"Http failure response for https://example.com:3000/api/mytargetapi: 0 Unknown Error"
"GET ... net::ERR_CONNECTION_REFUSED"
Everything was working smoothly until I deployed it on Plesk.
Here's my code of the request on the client side:
const params = new HttpParams().set('id', this.searchInDirectory);
this.http.get('https://example.com:3000/api/mytargetapi', {params})
.subscribe(response =>
{
this.itemDisplay = response;
}
server side:
var app = express();
app.disable('x-powered-by');
app.use(bodyParser.json());
app.use(cors());
app.use(routes);
const port = process.env.PORT || 3000;
const host = 'localhost';
const server = http.createServer(app);
server.listen(port, host, () => {
console.log(`Running on port ${port}`);
});
router.get('/api/mytargetapi', (req, res) => {
stuff here...
}
Does anybody know what am I doing wrong? I have searched the same question on stack overflow and followed each and every individual's solution but nothing seems to be working for me. Any help would be appreciated. Thanks!
I don't know how you're organizing your project structure for the back-end API, but you should test the API via POSTMAN before integration with your front-end client
var app = express();
app.disable('x-powered-by');
app.use(bodyParser.json());
app.use(cors());
app.get('/api/mytargetapi', (req, res) => {
stuff here...
}
app.use(routes);
const port = process.env.PORT || 3000;
const host = 'localhost';
const server = http.createServer(app);
server.listen(port, host, () => {
console.log(`Running on port ${port}`);
});

Error occurred while trying to proxy request [...] from 192.168.0.4:3000 to http://192.168.0.4:5000 (ECONNRESET)

I'm working on my first project using react and node and have been stuck on this problem for a while. I keep getting this error when trying to connect to my site using the ip address, but if I just do localhost:3000 it works perfectly. I want to be able to connect via the IP address so I can test it across devices. Here is the full error:
[HPM] Error occurred while trying to proxy request /socket.io/?EIO=3&transport=polling&t=N4EqtUl&sid=kz_I098ZM2h1Z6WZAAAI from 192.168.0.4:3000 to http://192.168.0.4:5000 (ECONNRESET) (https://nodejs.org/api/errors.html#errors_common_system_errors)
I checkout out this post and implemented setupProxy.js like the second answer suggested, but it still isn't working.
Here is my node server (index.js):
const express = require('express');
const app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
const path = require('path')
const port = process.env.PORT || 5000;
app.use(express.static(path.join(__dirname, 'client/build')));
io.on('connection', function(socket) {
console.log('a user connected');
});
// Anything that doesn't match the above, send back index.html
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname + '/client/build/index.html'))
})
http.listen(port || 5000, function () {
console.log('listening on', port);
});

Can I run 2 node.js express using https on 2 specific ports?

I am facing an issue that I am not able to solve alone. I am running 2 node.js server instances on my linux server, but the one running on port 4000 is running well, but the one running on the port 6000 is not working.
See below the example:
Port 4000:
Port 6000:
I checked my port on my server and everything seems to be ok:
See below my code for the 2 instances:
const express = require('express');
const cors = require('cors');
const fs = require('fs');
const https = require('https');
var app = express();
app.use(cors());
app.use(require('body-parser').json());
var privateKey = fs.readFileSync('/etc/letsencrypt/live/moreapp.com.br/privkey.pem', 'utf8');
var certificate = fs.readFileSync('/etc/letsencrypt/live/moreapp.com.br/fullchain.pem', 'utf8');
var credentials = {key: privateKey, cert: certificate};
var httpsServer = https.createServer(credentials, app);
app.get('/', (req, res) => {
res.send("Hello World");
});
httpsServer.listen(6000, () => {
console.log("Server Listening");
});
Could you please help to solve this?
Thanks
I think you should post the complete error for your request.
Which http code you get on port 6000 ?
If you call your servers through a firewall, did you correctly authorized requests on that port ?
EDIT :
Port 6000 seems to be a forbidden port. He is blocked by many browsers and maybe also by your http tool.
Try to change your port and try again.
Source : Chrome ports blocked
add this Error handler at end of your code , and check log what error your are getting
app.use((err, req, res, next) => {
console.log('error ==',err);
const error = err.message || 'Internal Server Error';
const status = err.status || 500;
res.status(status).json({ error: error });
})
read more about error handling here : https://expressjs.com/en/guide/error-handling.html
I think I got the problem. Actually is not with the express app, but with the port 6000, that get "ERR_UNSAFE_PORT" in browser, but works fine as API. Try changing the port, as 6000 is probably system-reserved.
You can read more in this issue

How to access a rest api in the local network using node js and express

I have created a REST server using Node.js and express, but i am having trouble connecting to the server on a separate machine. The server machine and client machine are on the same local network.
This is my code for the server
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
const PORT = process.env.PORT || 3000;
const imageUpload = require("./routes/imageUpload");
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', '*');
next();
});
app.use("/imageUpload", imageUpload);
app.listen(PORT, "0.0.0.0", () => {
console.log("Running at port " + PORT);
})
router.get("/getFilename", (req, res) => {
res.status(200).json({
status: "recieved"
});
});
Upon using postman on the server machine the get request works fine even if using the machine IP address in the network(192.168.0.104:3000/imageUpload/getFilename).
On a separate machine in the network the postman will give an error of There was an error connecting to 192.168.0.104:3000/imageUpload/getFilename. so i am unable to connect to the API, How do i fix this? Thank you very much
Your problem is rather in network settings, not in nodejs or express.
Did you try ping 192.168.0.104 your server? If it works most likely you need to manually open port 3000.
Try google how to open a port on a server for the OS your are using on that machine.

Putting socket.io behind a reverse proxy?

I recently decided to learn socket.io, to make something real-time. I wrote something up, following the Get Started page on the site, and tested it locally until I got it working properly.
I uploaded it to my server using the same process as anything else. I ran it on port 8002, and added it to my reverse proxy (using http-proxy-middleware) under /pong/*. I then proxied /socket.io/* to port 8002 before it worked. However after inspection with Firefox I noticed that socket.io was only using polling as a transport method and not websockets, and after some further thought I decided that sending /socket.io/* to 8002 is not going to be good when using socket.io on other projects in the future.
So I ask, how do I get multiple socket.io programs running behind a reverse proxy, using websockets as a for transport?
proxy.js
const express = require("express")
const fs = require('fs');
const http = require('http');
const https = require('https');
const proxy = require('http-proxy-middleware');
const privateKey = fs.readFileSync('/etc/[path-to- letsencrypt]/privkey.pem', 'utf8');
const certificate = fs.readFileSync('/etc/[path-to-letsencrypt]/cert.pem', 'utf8');
const ca = fs.readFileSync('/[path-to-letsencrypt]/chain.pem', 'utf8');
var credentials = {key: privateKey, cert: certificate, ca: ca};
var app = express();
app.use(function (req, res, next) {
console.log(req.url)
next()
})
app.use("/pong/*", proxy({ target: "http://localhost:8002", pathRewrite: {"^/pong": ""}, ws:true, changeOrigin: true }))
app.use("/pnw/war/*", proxy({ target: "http://localhost:8000" }))
app.use("/pnw/nation/*", proxy({ target: "http://localhost:8001" }))
app.use(express.static("./static"))
https.createServer(credentials, app).listen(443);
// Redirect all HTTP traffic to HTTPS
http.createServer(function (req, res) {
res.writeHead(301, { "Location": "https://" + req.headers['host'] + req.url });
res.end();
}).listen(80);
pong.js
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http, {
path: "/pong/"
});
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
http.listen(8002, function(){
console.log('listening on *:8002');
});
index.html
<script src="/pong/socket.io.js"></script>
<script>
var socket = io({
// transports: ['websocket'], upgrade: false, (using for testing)
path:"/pong"
})
// ...
</script>
What I have currently comes from following the answer to this question:
Setting up multiple socket.io/node.js apps on an Apache server?
However in the firefox console I get a warning which reads:
Loading failed for the <script> with source “https://curlip.xyz/pong/socket.io.js”, followed by an error io is not defined. In the network tab getting socket.io.js is showing a 404.
So what I believe is happening is that because express is capturing the requests for /, socket.io cannot (for some reason) server socket.io.js. However when I changed / to /index.html and loaded that there was no change.
So I did some more research and came upon a solution. I opened the port 8002 on my EC2 so that I could poke around looking for socket.io.js.
Essentially what I found is socket.io.js was located at /pong/pong/socket.io.js because I set path in pong.js to "pong", which, in hindsight make sense, the proxy adds one "pong", while socket.io itself is capturing "/pong".
Knowing this I removed the path option in pong.js, so that socket.io.js can be found at /pong/socket.io/socket.io.js. I then made the client point to this by changing the script tag and path option in index.html.
pong.js
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
http.listen(8002, function(){
console.log('listening on *:8002');
});
index.html
<script src="/pong/socket.io/socket.io.js"></script>
var socket = io({
path:"/pong/socket.io/"
})

Resources