Connection to nodejs (express) refused

Hello everyone,

I’m a long term programmer, but haven’t used nodejs much in my code. Now I need to use it in my current code and I’ve ran into a problem that I can’t seem to figure out myself, I have googled a lot but nothing seem to fix it.

I am trying to get my website to connect to the nodejs server running on same host.

If I visit the url in my browser, it works fine (http://localhost:6857/ and I see this respond


But when I try to connect thru the website, I just get

GET http://localhost:6857/ net::ERR_CONNECTION_REFUSED

This is my server.js

// use express
var express = require("express");
// create instance of express
var app = express();

// use http with instance of express
var http = require("http").createServer(app);

// start the server
var port = 6857;
http.listen(port, '', function () {
    console.log("Listening to port " + port);

// create socket instance with http
var io = require("")(http);

// add listener for new connection
io.on("connection", function (socket) {
    // this is socket for each user
    console.log("User connected",;

io.on("connect_error", (err) => {
  console.log(`connect_error due to ${err.message}`);

And this is my JS code inside my website

    var server = "http://localhost:6857/";
    var io = io(server);

Can someone guide my in the right direction for how to fix this, so I can begin using nodejs inside my website?

Show comments

Submit an answer
Answer a question...

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
April 3, 2022


Rather than using localhost try with the server IP so that your frontend could connect to the backend service on the server. Otherwise the fronted would try to connect to your PC rather than the localhost on the server.

Let me know how it goes!

Also you could install Nginx and use it as a reverse proxy so that you don’t have to specify the 6857 port each time, here is a quick video that shows you how to do that:



Socket IO requires you to explicitly enable CORS. See:

This comment has been deleted