skip to Main Content

I have some node.js code that I am running called app.js When it runs is gives an error saying connection refused?

It is running on plesk so I cannot debug it. I have added the package.json too but maybe I am missing something?

package.json:

{
  "name": "socket-example",
  "version": "0.0.1",
  "description": "test socket.io app",
  "dependencies": {
    "socket.io": "^1.7.3"
  },
  "scripts": {
    "start": "node app.js"
  }
}

app.js:

var http = require('http');
var fs = require('fs');


// Loading the index file . html displayed to the client
var server = http.createServer(function(req, res) {
    fs.readFile('./index.html', 'utf-8', function(error, content) {
        res.writeHead(200, {"Content-Type": "text/html"});
        res.end(content);
    });
});

// Loading socket.io
var io = require('socket.io').listen(server);

// When a client connects, we note it in the console
io.sockets.on('connection', function (socket) {
    console.log('A client is connected!');
});


server.listen(8080);

index.html:

<body>
    <h1>Communicating with socket.io!</h1>

    <p><input type="button" value="Poke the server" id="poke" /></p>


    <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
    <script src="/socket.io/socket.io.js"></script>
    <script>
        var socket = io.connect('http://localhost:8080');

        // The visitor is asked for their username...
        var username = prompt('What's your username?');

        // It's sent with the signal "little_newbie" (to differentiate it from "message")
        socket.emit('little_newbie', username);

        // A dialog box is displayed when the server sends us a "message"
        socket.on('message', function(message) {
            alert('The server has a message for you: ' + message);
        })

        // When the button is clicked, a "message" is sent to the server
        $('#poke').click(function () {
            socket.emit('message', 'Hi server, how are you?');
        })
    </script>
</body>

2

Answers


  1. Try changing all you code like this as this will resolve your issues
    app.js

    var express = require('express');
    
    var app = express();
    
    app.set("view engine","html");
    
    
    var http = require('http');
    
    //making express server for routes
    var server = http.Server(app);
    
    
    
    
    // Loading socket.io
    var io = require('socket.io')(server);
    
    //index route
    //loaded when the website is loaded root route.
    app.get('/',function(req,res){
    	//specify the path to the directory (assuming both are in the same directory)
    	res.sendFile(__dirname + '/index.html');
    })
    
    // When a client connects, we note it in the console
    io.sockets.on('connection', function (socket) {
        console.log('A client is connected!');
    
        //message of client side
        socket.on('little_newbie',function(message){
        	//give the username in terminal of the connected client
        	console.log(message);
        	//send a hello to the client from the server.
        	io.sockets.emit('message',"Hello "+message);
        })
        //button poke mesage response
        socket.on('message',function(message){
        	//print the hello message from the server
        	console.log(message)
    
        	//after this you can send a response back to the client as in the above case
        	io.sockets.emit('poke',"Hi I am fine ");
        })
    
    });
    
    server.listen(8080);
    <!DOCTYPE html>
    <html>
        <head>
        <meta charset="utf-8" />
        <title>Socket.io</title>
    </head>
    
    <body>
        <h1>Communicating with socket.io!</h1>
    
        <p><input type="button" value="Poke the server" id="poke" /></p>
    
    
        <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
        <script src="/socket.io/socket.io.js"></script>
        <script>
            var socket = io.connect('/');
    
            // The visitor is asked for their username...
            var username = prompt('What's your username?');
    
            // It's sent with the signal "little_newbie" (to differentiate it from "message")
            socket.emit('little_newbie', username);
    
            // A dialog box is displayed when the server sends us a "message"
            socket.on('message', function(message) {
                alert('The server has a message for you: ' + message);
            })
    
            // When the button is clicked, a "message" is sent to the server
            $('#poke').click(function () {
                socket.emit('message', 'Hi server, how are you?');
            })
    
            socket.on('poke',function(message){
                alert(message);
            })
        </script>
    </body>
    {
      "name": "socket-example",
      "version": "0.0.1",
      "description": "test socket.io app",
      "dependencies": {
        "express": "^4.16.4",
        "socket.io": "^1.7.3"
      },
      "scripts": {
        "start": "node app.js"
      }
    }
    Login or Signup to reply.
  2. There ate no routes configuration , and when you try to ping from browser it won’t connect . just like meaning less URI. Try making a route and then calking route from Browser.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search