Question

I have set up a server in node.js using socket.io and epxress.

When I set it up as shown below, it works like charm (now it's listening on 8080).

var express = require("express");
var http = require("http");
var io = require("socket.io");

var app = express();
var server = http.createServer(app).listen(port, host);
var scoketIO = io.listen(server);

But I need it to listen to different port, and if I try eg. 8000

var app = express();
var server = http.createServer(app).listen(port, host);
var scoketIO = io.listen(8000);

I get the following error: GET http://10.0.33.34:8080/socket.io/socket.io.js 404 (Not Found). Can anybody please help me?

Was it helpful?

Solution

var express = require("express");
var http = require("http");
var io = require("socket.io");

var app = express();
var port = 8000;
var server = http.createServer(app).listen(port, host);
var scoketIO = io.listen(server);

Above code is not tested but most likely it will not fail. Pass port value to listen method of http.createServer(app).

OTHER TIPS

var express = require('express')
http = require('http');
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);

server.listen(8000);

//Code needs to be in this order

and check in the script that the port is 8000

<script src=""></script>

Do you include the script in you html ?

Maybe change the include to the port 8000.

<script src="http://10.0.33.34:8000/socket.io/"></script>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top