Service || Server

Nodejs : normalizePort

조아덕 2021. 7. 15. 14:16
normalizePort 

 

제공된 포트가 number, 숫자가 아니면 숫자 string이고 다른 것이 있으면 false로 설정 되었는지 확인하는 안전 레일가드

normalizePort이 기능은 Express 팀의 상용구인 express-generator 에 도입됨.

/**
* Get port from options and store in Express.
*/

const port = normalizePort(process.env.PORT || '8080');
CApp.set('port', port);
/**
 * Normalize a port into a number, string, or false.
 */
function normalizePort(val) {
  var port = parseInt(val, 10);

  if (isNaN(port)) {
    // named pipe
    return val;
  }

  if (port >= 0) {
    // port number
    return port;
  }

  return false;
}

 


Ref.

https://stackoverflow.com/questions/55988881/what-does-normalizeport-function-do-in-nodejs