RESTful APIs using NodeJS, ExpressJS, and MS SQL Server
RESTful APIs using NodeJS, ExpressJS, and MS SQL Server
REST (REpresentational State Transfer) is an architectural style for developing web services that uses HTTP. REST server provides access to resource and REST client modifies the resources. REST is popular due to its simplicity. REST uses different representation methods to represent resources like JSON, XML and text but most popular one is JSON.
Advantages of Restful services
- Language independent architectural style
- simple
- reliable
- scalable
- implementation is easy
NodeJS is a very popular and powerful JavaScript framework. we can develop attractive web applications like video streaming sites, single page web applications, etc. And Node is open source.
Advantages of NodeJS
- Easy implementation
- Lightweight, fast and scalable
- streaming capability
- high performance
- real-time web applications
- Community friendly
ExpressJS is also written in JavaScript and it is also open source. We can use ExpressJS for build web applications and APIs. It has been called the de facto standard server framework for Node.js.
Advantages of ExpressJS
- Has the biggest Community
- Most matured framework
- simple
- code re usability
- easy implementation
- allows you o define error handling middle-ware.
Let's start to implement a RESTful Web Service,
Step 1 :- Create a package.json file. That file contains the initial configuration and we can use following commands to create that.
npm init or npm init -y
npm init or npm init -y
Step 2 :- Create server.js file. That contains server configurations and initialization of database connection and also it contains APIs as your requirement.
Example:-
//Initiallising node modules var express = require("express"); var bodyParser = require("body-parser"); var sql = require("mssql"); var app = express(); // Body Parser Middleware app.use(bodyParser.json()); //CORS Middleware app.use(function (req, res, next) { //Enabling CORS res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, contentType,Content-Type, Accept, Authorization"); next(); }); //Setting up server var server = app.listen(process.env.PORT || 8080, function () { var port = server.address().port; console.log("App now running on port", port); }); //Initiallising connection string var dbConfig = { user: “<dbUserName>”, password: “<dbPassword>”, server: “<dbHost_URL>”, database:” <dbName>” }; //Function to connect to database and execute query var executeQuery = function(res, query){ sql.connect(dbConfig, function (err) { if (err) { console.log("Error while connecting database :- " + err); res.send(err); } else { // create Request object var request = new sql.Request(); // query to the database request.query(query, function (err, res) { if (err) { console.log("Error while querying database :- " + err); res.send(err); } else { res.send(res); } }); } }); } //GET API app.get("/api/user", function(req , res){ var query = "select * from [user]"; executeQuery (res, query); }); //POST API app.post("/api/user", function(req , res){ var query = "INSERT INTO [user] (Name,Email,Password) VALUES (req.body.Name,req.body.Email,req.body.Password”); executeQuery (res, query); }); //PUT API app.put("/api/user/:id", function(req , res){ var query = "UPDATE [user] SET Name= " + req.body.Name + " , Email= " + req.body.Email + " WHERE Id= " + req.params.id; executeQuery (res, query); }); // DELETE API app.delete("/api/user /:id", function(req , res){ var query = "DELETE FROM [user] WHERE Id=" + req.params.id; executeQuery (res, query); });
Step 3 :- I recommend the Visual studio code to do the implementation. If not you can run those commands using CMD and if using VS Code you can run commands using VS Code terminal. Run npm install command to that installs all the necessary node modules. After that run node server.js command and it will starts the server on localhost:8080.
Step 4 :- We can test our APIs using API testing tool like katalon studio, postman, apigee, etc. I recommend postman to do the API testings. It is open source and easy to use.
Following video will give you brief idea about node APIs using ExpressJs,
References
Step 4 :- We can test our APIs using API testing tool like katalon studio, postman, apigee, etc. I recommend postman to do the API testings. It is open source and easy to use.
Following video will give you brief idea about node APIs using ExpressJs,
References
[1] Building RESTful APIs using Node JS, Express JS, and MS SQL Server - https://vmokshagroup.com/blog/building-restful-apis-using-node-js-express-js-and-ms-sql-server/
Good post helpful for everyone.
ReplyDeleteFull Stack online Training
Full Stack Training
Full Stack Developer Online Training
Thank you for the informative post about RESTful APIs using NodeJS, ExpressJS, and MS SQL Server. Found it useful
ReplyDeleteNode JS Online training Node JS training in Hyderabad
I feel there is a need to provide some more information about REST API and Power BI and also look for some more similar aspects.
ReplyDeleteSQL Server Load Rest API
informative blog, keep updating, i would like to share my knowledge about full stack course in satara thankyou.
ReplyDelete