ExpressJS: normalizePort function is really necessary?

Viewed 3249

I used express-generator for a new project.

In file bin/www exists the function normalizePort:

...

var http_port = 3000;

var port = normalizePort(process.env.PORT || http_port);
app.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;
}

Question:

Is really necessary the function or I can will simply?

app.set('port', parseInt(process.env.PORT, 10));

The function is generic and avoid possibles errors?

I use dotenv and dotenv-safe module for load my file .env

1 Answers

normalizePort doesn't quite behave like parseInt. You can pass an string of (non-numeral) text into it and get that same string back, which may be necessary in some very rare cases. If you're passing in all the values yourself through the life of the project, you probably don't need either normalizePort or parseInt.

Related