Photo by Chris Ried on Unsplash

How to quickly create a simple REST API for SQL Server database

Luc Lamontagne
Feb 13, 2017 · 1 min read

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

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

Simple Select query on the Customer table

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

Simple Select query with a where clause on the CustomerId field

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

Execute a stored procedure with one parameter

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

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

Vooban's tech stories

Stories written by the software team at Vooban

Luc Lamontagne

Written by

Vooban's tech stories

Stories written by the software team at Vooban

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade