express.js,我如何区分http服务器和https服务器?

时间:2017-09-21 02:26:13

标签: node.js express

我想在服务器启动时在控制台中记录信息。

这样的信息:

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

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

这是我的代码:

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);
}

1 个答案:

答案 0 :(得分:2)

Express有req.secure这是一个布尔属性,如果建立了TLS连接,则为true。