You are on page 1of 3

3/15/22, 2:46 PM How to quickly create a simple REST API for SQL Server database | by Luc Lamontagne | Vooban's

gne | Vooban's tech stories | Medium

Upgrade Open in app

Published in Vooban's tech stories

Luc Lamontagne Follow

Feb 14, 2017 · 1 min read · Listen

Photo by Chris Ried on Unsplash

How to quickly create a simple REST API for SQL Server database
In IT consulting projects, we frequently have to use an existing database. Here’s how to create a simple REST API for a database in
SQL Server using Node.js and the two modules Express (a Web framework for Node.js) and mssql (MS SQL Server client for
Node.js). Returning results in JSON format that can easily be treated by a web application, mobile, another REST service, etc.

Server initialization

1 var express = require('express'); // Web Framework


2 var app = express();
3 var sql = require('mssql'); // MS Sql Server client
4
5 // Connection string parameters.
6 var sqlConfig = {
7 user: 'UserName',
8 password: 'mot de passe',
9 server: 'localhost',
10 database: 'DatabaseName'
11 }
12
13 // Start server and listen on http://localhost:8081/
14 var server = app.listen(8081, function () {
15 var host = server.address().address
16 var port = server.address().port
17
18 console log("app listening at http://%s:%s" host port)
https://medium.com/voobans-tech-stories/how-to-quickly-create-a-simple-rest-api-for-sql-server-database-7ddb595f751a 1/3
3/15/22, 2:46 PM How to quickly create a simple REST API for SQL Server database | by Luc Lamontagne | Vooban's tech stories | Medium
18 console.log( app listening at http://%s:%s , host, port)
19 });
Upgrade Open in app
initialisation.js
hosted with ❤ by GitHub view raw

*Note: Make sure the SQL Server Browser windows service is running.

Simple Select query on the Customer table

Ex .: http://localhost:8081/customers/

1 app.get('/customers', function (req, res) {


2 sql.connect(sqlConfig, function() {
3 var request = new sql.Request();
4 request.query('select * from Sales.Customer', function(err, recordset) {
5 if(err) console.log(err);
6 res.end(JSON.stringify(recordset)); // Result in JSON format
7 });
8 });
9 })

executer-select.js
hosted with ❤ by GitHub view raw

Simple Select query with a where clause on the CustomerId field

Ex .: http://localhost:8081/customers/1

1 app.get('/customers/:customerId/', function (req, res) {


2 sql.connect(sqlConfig, function() {
3 var request = new sql.Request();
4 var stringRequest = 'select * from Sales.Customer where customerId = ' + req.params.customerId;
5 request.query(stringRequest, function(err, recordset) {
6 if(err) console.log(err);
7 res.end(JSON.stringify(recordset)); // Result in JSON format
8 });
9 });
10 })

executer-filtrer.js
hosted with ❤ by GitHub view raw

Execute a stored procedure with one parameter

Ex .: http://localhost:8081/customers/1/orders

1 app.get('/customers/:customerId/orders', function (req, res) {


2 sql.connect(sqlConfig, function() {
3 var request = new sql.Request();
https://medium.com/voobans-tech-stories/how-to-quickly-create-a-simple-rest-api-for-sql-server-database-7ddb595f751a 2/3
3/15/22, 2:46 PM How to quickly create a simple REST API for SQL Server database | by Luc Lamontagne | Vooban's tech stories | Medium
q q q ();
4 request.input('CustomerId', req.params.customerId);
Upgrade Open in app
5 request.execute('Sales.uspShowOrderDetails', function(err, recordsets, returnValue, affected) {
6 if(err) console.log(err);
7 res.end(JSON.stringify(recordsets)); // Result in JSON format
8 });
9 });
10 })

stored-proc.js
hosted with ❤ by GitHub view raw

This implementation remains minimalist. But with the information received in the callback, it would be easy to add logging, manage
SQL errors, etc.

You can try this example by simply copy the code in a file like server.js and run it with Node.js:

$ node server.js

And then make the REST calls in a Web browser, a tool like Postman or, if you prefer not leaving your console, use curl with a
command like:

$ curl -X GET http://localhost:8081/Customers

https://medium.com/voobans-tech-stories/how-to-quickly-create-a-simple-rest-api-for-sql-server-database-7ddb595f751a 3/3

You might also like