0

I wasn't sure if this is appropriate on the Ubuntu exchange or here but it is mostly code related i believe.

So I have created a neat working little web-chat application using socket.io which I've been developing on my Win10 pc using Git-bash and running it on my localhost with node.js and everything has been working just fine.

Thus I have come to a point where I would like to try out my app on my web-server.

Lets get more detailed what my Problems are:

  1. I am unsure of how my server.js file should listen or open up the ports so to speak. I have tried looking in the sockets.io chat example and tried their method yet the '../..' module leaves me confused.
  2. Following the express.js site tutorial I actually get my server to respond listening on port 3000 yet my website returns the no socket.io/socket.io.js found.
  3. this and this just lead to another localhost tutorial

In Short I have come to the point where when i do node server.js It seems to start listening here's the code to that part:

  var express = require('express');
    var http = require('http');
    var app = express();
    var server = http.createServer(app);
    var io = require('socket.io').listen(server);
    var port = process.env.PORT || 3000;

    app.get ('/', function(req,res) {
     res.sendFile(__dirname + '/index.html');
    });

    app.use("/static", express.static('./static/'));
    server.listen(3000);

But now the problem is is that my html file cannot seem to find the socket.io/socket.io.js file, even though i have installed sockets.io, i consulted to this stack question which should fix it, but it didnt thus leads me to believe theres a thicker more server side issue?

Versions i am using:
  express: **4.16.4**;
  node   : **10.15.0**;
  npm    : **6.4.1**;
  socket : **2.2.0**;

EDIT: Added my html code snips

html

  <script src="/socket.io/socket.io.js"></script>
        <script src="https://code.jquery.com/jquery-1.11.1.js"></script>
        <script src="static/index.js"></script>
EricTalv
  • 1,000
  • 1
  • 13
  • 26
  • 1
    Where did you get your `var io = require('socket.io').listen(server);` line from? In my opinion, it should be `var io = require('socket.io')(server);`. – Azami Jan 16 '19 at 22:55
  • Hm, seems like an honest mistake, going back and forward changing the code. Didn't seem to solve my issue though. – EricTalv Jan 16 '19 at 23:04
  • 1
    I always get confused about `socket.io` implementation to be honest. Right now, I have a running one working where I make Express listen on a specific port (say, `server.listen(1111)`) and socket.io on another one (`io.listen(3000)`). If this can be used of any use to you ... – Azami Jan 16 '19 at 23:08
  • Huh, so you have them still on like on server right? But what do you mean by having express and socket.io listen on two different ports? Don't they have to be on the same endpoints to communicate? + I know my express should be listening on 3000 with the code i have but I am not sure how I would do this in on the socket.io part. – EricTalv Jan 16 '19 at 23:15
  • 1
    I have, like you, `var server = http.createServer(app);`, `var io = require('socket.io')(server);` and `server.listen(port)`, I simply added `io.listen(3000)`. Both Express and socket.io listen to their own port and everything runs smoothly. Could you add your client code to see your ` – Azami Jan 16 '19 at 23:24
  • 1
    I put your code without changing a single character into a file and put it in a folder on my Raspberry running Lubuntu. I run the file, then I go to `http://192.168.220.11:3000/socket.io/socket.io.js` on my PC and I see the script just fine. I did notice a small error though: going to `http://192.168.220.11:3000` results in an error because express is trying to serve `...pathindex.html` instead of `...path/index.html`; you need to add a slash `/index.html` to your `/` route. –  Jan 16 '19 at 23:39
  • I see you are running it off an ip where you can change the port in the browser, i though have a domain thus i cant change it like that, do i have to dive into my domain IP files and change the port there? + Thanks for the point out on the / <- but that turned out to be only a post mistake. As for the io.listen, i tried it, at first it seemed to get a response but now when i try it again all ports i choose seem to be in use.. strange. – EricTalv Jan 16 '19 at 23:47
  • 1
    `i though have a domain thus i cant change it like that` not sure what you mean, you can just go to `http://example.com:3000` Don't you have to do that anyway to see the index file? The raspberry also has a hostname; everything works just as well when I go to http://raspberry.router.box:3000 –  Jan 16 '19 at 23:53
  • haza! My sheer ignorance diluded me.. I thought i could just run it on my server and it would pop right open when i typed in example.com... Wait but how Would i get it working like that? Without the need of putting the Port behind it? – EricTalv Jan 17 '19 at 00:03
  • 1
    Most servers have Apache running on port 80; that's the default one if you don't state a port. Which means you saw the index.html from your apache server, not from express. Consequently, the browser was requesting `/socket.io/socket.io.js` from Apache, too, resulting in a 404. –  Jan 17 '19 at 00:05
  • 1
    If you want to run express on port 80, you need to kill the apache service, then change the port in your node code to `80`. –  Jan 17 '19 at 00:06

0 Answers0