codingIsFun
codingIsFun

Reputation: 91

Node.js/Socket.io: process.env.port gives error

I have this:

var io = require('socket.io');
var port = process.env.port || 1337;

and later:

socket = io.listen(port)

then on:

socket.sockets.on('connection', ...)

I get the error:

TyoeError: server.on is not a function on socket.io/lib/manager.js:104

server.on('error, function(err) {
       ^

But if I set:

var port = 1337;

It works fine. How do I fix so it works with process.env.port?

Upvotes: 0

Views: 1306

Answers (1)

CherryDT
CherryDT

Reputation: 29062

Probably the socket library doesn't expect the port to be a string (which it will be when coming from an environment variable).

Try converting it to a number first:

var port = Number(process.env.port) || 1337;

Upvotes: 2

Related Questions