2017-09-21 126 views
0

我想在服務器啓動時在控制檯中記錄信息。express.js,如何區分http服務器和https服務器?

這樣的信息:

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

回答

2

Express有req.secure這是一個布爾值屬性,它是真實的,如果TLS連接成立。

+0

雖然這在'聆聽'事件中不可用,但這是OP詢問的內容。 – Paul