Practical Guide To Create REST API using Node.js and MS-Sql- Part III

Hello everyone,

In this article, we will see how to setup environment files and also how to send customized objects as a response.

Setting up the Environment Files:

Now, let us see how we can maintain our Local and Server related configurations dynamically.

Our project or application may pass through different stages like Development, Staging, Production, etc. and according to that, we can manage all the configurations.

Here, we will make a folder as “config” and inside this folder create one folder for “environment”. Now inside this environment folder, create 2 files as development.json and production.json.

Inside development.json file we can have an object “server” which has all the local configurations for development like hostname, port number, etc.

{
"server": {
"host": "0.0.0.0",
"port": "4001"
}
}

And for production.json, right now we are just changing the port number to 9001:

{
"server": {
"host": "0.0.0.0",
"port": "9001"
}
}

But now the question here is, how it will come to know which environment — Development or Production to be used?

So the solution is, we will pass a flag as ‘production’ from the terminal when we run this application to tell that we want Production environment and nothing will be passed as a flag if we want Development environment.

Now create a new file index.js inside this environment folder to fetch the value of this flag.

const ENV = process.argv[2] || 'development';const config = require('./' + ENV);module.exports = config;

In the above code, argv is an array that has all values we passed from the terminal. For example if we pass node index.js from terminal then argv array will have 2 values: argv[0] = node and argv[1] = index.js.

And if we also pass the flag ‘production’ from the terminal — node index.js production then it will have argv[2] = production.

Now, on our index.js file (of the root level), instead of writing all the configurations hardcoded, we will make use of these above files.

So, add the reference of our environment files like below and also use that config object inside this server.listen() method :

const app = require('./app');
const config = require('./config/environment');
const server = require('http').Server(app);
server.listen(config.server.port,config.server.host, ()=> {
console.log('Server Started');
});

So, it’s time to run the app.

First, we will run with Development environment. So we will write only node index.js on the terminal.

And for the Development we will use port 4001:

Now, to run with Production environment, we will write node index.js production on the terminal.

And for the Production we will use port 9001:

Now same way we can also write configurations of the Database in environment files.

development.json

And instead of using dbconnection.js file we will now create a new separate file. So create a folder as utilities and inside this folder create a file as mssqlconnection.js like below.

In this file, we are passing an object to getConnection() method and that object will set the configuration values. And we can now delete that dbconnection.js file as it is not needed.

Now, we will create a global instance of this mssqlconnection.js file on app.js so that our entire application can use that same instance.

Here, we have just created two global objects: CONFIG and MSSQLConnection.

CONFIG object is for getting the environment run time and using it globally and MSSQLConnection object will store the Database related configurations globally.

So let us open the product.mssql.js file and remove the import statement of dbconncetion.js file and inside all the methods, we will use global.MSSQLConnection object to connect to the Database.

Let us run this application using node index.js and we can see it is working the same as before.

Summary diagram

Sending Customized Response:

Instead of sending the default response of the Database as it is to the user we can modify or customize it.

We will send a customized object as a Response to the user.

For this, we will create a new file as response.js inside the utilities folder and here we will have a method bindResponse() which will return the customized object.

Now, on our app.js file we will initialize this Response class as below:

async function init() {…………const Response = require(‘./utilities/response’);new Response(app);…………}

Now, on our product.js file we will call sendResponse() method of the Response class to send the response to the user.

…………async getAllProducts(req, res) {try {const output = await productMssql.getAllProducts();res.sendResponse(output);}catch (error) {console.log(error);}}…………

Let us run the app using node index.js and we can see the output as below:

Summary diagram:

So, now in the next part of the series, let’s see how to handle Errors and Exceptions and how to implement Encryption in our application. Because that has now been so basic functionality to implement. Almost a necessity. Right!?

Jinal Shah is corporate trainer on different technology like node.Js, Angular,Ionic 2, BOT Framework etc.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store