Lin Du
Lin Du

Reputation: 102207

express.js, How can I distinguish http server and https server?

I want to log a information in console when the server is started.

the information like this:

http server is started, address: http://127.0.0.1:2223

https server is started, address: https://127.0.0.1:2222

here is my code:

httpsServer.on('listening', () => onListening(httpsServer));
httpServer.on('listening', () => onListening(httpServer));

function onListening(server: http.Server | https.Server) {
  const addr = server.address();
  let protocoal: string;

  //Here, I want to distinguish https and http server.
  //Is there nodeJs/express.js way rather than pass a parameter way?
  //like `server.secure` api?

  protocol = server.secure ? 'https' : 'http';

  console.log(protocoal + ' server is started,address:' + addr.address + ':' + addr.port);
}

Upvotes: 0

Views: 57

Answers (1)

Swaraj Giri
Swaraj Giri

Reputation: 4037

Express has req.secure which is a boolean property that is true if a TLS connection is established.

Upvotes: 2

Related Questions