1. Overview

In this article, we will learn how to change node.jsserver port. We will explore the different techniques to change the port.

  1. By Setting The environment variable.
  2. Using the configuration file.
  3. Using Command Line Arguments.

2. Example

2.1 Using The Environment Variable

First of all, You need to set an environment variable using this command.

set PORT=8080

Now our  server.js will look like this. Here we used environment variable port if that is not set then it will use 8081 as the default port.

var express  = require('express')

var app  = express()

var server = app.listen(process.env.PORT || 8081, () => {
    console.log('Server is started on 127.0.0.1:'+ (process.env.PORT || 8081))
})

If you don’t want to set an environment for port independently then we can pass environment variable run time like shown below.

SET PORT=8080 && node server.js

2.2 Using The Configuration File

likewise port we can set the environment which environment to chose at runtime, for example, production or development. Based on the environment configuration property will set.

2.2.1 Project Structure

Configuration Files Structure

Configuration Files Structure

2.2.2 development.js or production.js

development.js will look like this.

module.exports = {
    Host :{
        ip: '192.1.200.177'
    },
    Logger: {
        mode: debug
    },
    .....// contains other details like database connection etc..
}

similarly, set your production.js. You can also set some default.js which contains some default or common configuration.

2.2.3 server.js

Set the environment variable and run the server.js.

set NODE_ENV = production

server.js will look like this. It will import the configuration based on NODE_ENV.

var express  = require('express')
require('./config/'+(process.env.NODE_ENV || 'development')+'.js')
var app  = express()

var server = app.listen(process.env.PORT || 8081, () => {
    console.log('Server is started on 127.0.0.1:'+ (process.env.PORT || 8081))
    console.log('Environment set to '+ process.env.NODE_ENV)
})

2.3 Using Command Line Arguments

Now by using the command line argument, we can set port and other properties. Let’s set run the server using this command

node server.js 8080

To fetch the argument, you can use process.argv . which stores all the argument likewise.

var express  = require('express')
var app  = express()

var server = app.listen(process.env.PORT || 8081, () => {
    console.log('Server is started on 127.0.0.1:'+ (process.argv[2] || 8081))
    console.log('Environment set to '+ process.argv[3])
})

Here, argv[0] contains node.exe path. argv[1] contains server.js path because of this we use argv[2] directly for port and argv[3] for environment setup.

3. Conclusion

As a result of this article, we learned how to configure different server properties. Using three different kinds of methods.

4. References

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No
Tags: ,

Leave a Reply

Your email address will not be published. Required fields are marked *