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

Hello Everyone,

This article series will help you in building REST APIs using nodeJS and Express. Yes, using nodeJS you will connect to your Database Server (of any platform). So let’s dive into it and learn all these here!!

Topics to be covered:

  • Basic ‘Hello World’ Application
  • CRUD Operation using MsSql Database
  • Setting up the Environment Files
  • Sending Customized Response

But, before we dive deeper and start with our first demo, let us understand some fundamentals.

What is REST API?

• REST stands for REpresentational State Transfer.

• REST APIs are an approach for communication purposes used in various web service development.

REST API methods:





Features of REST APIs:

• Simple than SOAP

• Stateless

• Cachable

• Layered System

What is a call back function?

A call back function is a function passed into another function as an argument.

const server = http.createServer( (req,res)=>{

res.send(‘hello world’);


Here, the createServer function has one function as an argument. So, createServer is an outer function and the function inside creatServer is known as the inner function. When the createServer function will be called, automatically inner function will be called too.

Now, our inner function has two arguments: req and res

What is req?

Req is an abbreviation of request. Req is an object containing information about the HTTP request that raised the event.

What is res?

Res is an abbreviation of response. In response to Req, the res is used to send the response back to the desired HTTP response.

Now, the next thing you will need is node js installed in your machine. To download node js follow the below-stated link:

Download and install recommended for most users' version. Please take note that the version of node js might be different when you read this blog.

Once, installed have an assurance that it has been correctly installed. Open the command prompt and fire the below-mentioned command.

Once everything is ready, we are good to go!

Let’s start!!!!

Basic ‘Hello World’ Application:

1) First create a new folder and give it your application name.

2) Generate a package.json using the following command:

npm init

3) Now before we start with nodejs, we first create a Web Server for handling our HTTP requests. So open this nodeRESTAPI app in Visual Studio Code (your IDE) and now in package.json add the “dependencies” tag for “http”:

"dependencies" : {"http": "0.0.1"}

And then run the command “npm install” to install this dependency.

Another way to add this dependency is, you can directly run the command “npm install http” from your terminal and it will update the package.json file automatically.

Like this, you can also add a dependency for “http”.

4) Now we will create a new file called “index.js” at the root level of our app.

Here we will use this “http” package as below:

const http = require('http');const server = http.createServer((req,res) => {
res.end('Hello World');

You can see that we have included http package and then using http, we called createServer() method with 2 parameters: Request and Response and inside this we are printing the response as “Hello World”.

5) Now run this app by the following command and also open the browser with http://localhost:3000/

cmd> node index.js

Alright, so our basic “Hello world” application is running successfully now. Let us move ahead and see how to perform basic CRUD operation with MsSql in part-2 of this series.

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