David Hoareau
David Hoareau

Reputation: 53

Can I run 2 node.js express using https on 2 specific ports?

I am facing an issue that I am not able to solve alone. I am running 2 node.js server instances on my linux server, but the one running on port 4000 is running well, but the one running on the port 6000 is not working.

See below the example:

Port 4000:

enter image description here

Port 6000:

enter image description here

I checked my port on my server and everything seems to be ok:

enter image description here

See below my code for the 2 instances:

const express = require('express');
const cors = require('cors');
const fs = require('fs');
const https = require('https');

var app = express();
app.use(cors());
app.use(require('body-parser').json());

var privateKey  = fs.readFileSync('/etc/letsencrypt/live/moreapp.com.br/privkey.pem', 'utf8');
var certificate = fs.readFileSync('/etc/letsencrypt/live/moreapp.com.br/fullchain.pem', 'utf8');
var credentials = {key: privateKey, cert: certificate};
var httpsServer = https.createServer(credentials, app);

app.get('/', (req, res) => {
  res.send("Hello World");
});

httpsServer.listen(6000, () => {
    console.log("Server Listening");
});

Could you please help to solve this?

Thanks

Upvotes: 0

Views: 234

Answers (3)

Luiz Fernando da Silva
Luiz Fernando da Silva

Reputation: 1499

I think I got the problem. Actually is not with the express app, but with the port 6000, that get "ERR_UNSAFE_PORT" in browser, but works fine as API. Try changing the port, as 6000 is probably system-reserved.

You can read more in this issue

Upvotes: 0

Jérôme
Jérôme

Reputation: 56

I think you should post the complete error for your request. Which http code you get on port 6000 ?

If you call your servers through a firewall, did you correctly authorized requests on that port ?

EDIT : Port 6000 seems to be a forbidden port. He is blocked by many browsers and maybe also by your http tool. Try to change your port and try again.

Source : Chrome ports blocked

Upvotes: 2

Saurabh Mistry
Saurabh Mistry

Reputation: 13679

add this Error handler at end of your code , and check log what error your are getting

app.use((err, req, res, next) => {
    console.log('error ==',err);
    const error = err.message || 'Internal Server Error';
    const status = err.status || 500;
    res.status(status).json({ error: error });
})

read more about error handling here : https://expressjs.com/en/guide/error-handling.html

Upvotes: 0

Related Questions