1

I'm trying to build a webproject that works fine on localhost locally, but doesn't work when I upload it to heroku. I've added a Procfile with the text: web: npm start which correctly runs node index.js as it's supposed to. However, when it gets to listening to a port, it recieves way to high numbers as the port number (in the range of 300.000 and up). Here's an example from last run: RangeError [ERR_SOCKET_BAD_PORT]: options.port should be >= 0 and < 65536. Received 350251..

Here is my code for listening:

httpsServer = https.createServer(credentials, app)
httpServer = http.createServer(app)


const httpPort = process.env.PORT || 80;
const httpsPort = process.env.PORT + 1 || 443;

app.use(express.static(__dirname + '/.webpages/' + webpage + '/public'))
httpsServer.listen(httpsPort, () => console.log('HTTPS server listening on port ' + httpsPort))
httpServer.listen(httpPort, () => console.log('HTTP server listening on port ' + httpPort))

Is this a known issue or am I doing something wrong?

2

From what it looks like, process.env.PORT is interpreted as a string then adding +1 concatenates the value, and then gets translated to an integer, this might be causing the issue.

Ex:

'35025' + 1 = '350251' which coincides with the out of range port number.

you should not be adding +1 to the port number variable

1
  • Thank you a lot. Nice catch! I would probably have been going at it for at least another few hours!
    – Eric B
    Aug 26 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.