Skip to content

blurbyte/restful-quickstart

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Welcome to RESTful Quickstart

RESTful Quickstart is complete Node.js API hooked to Azure SQL database. It's based on modern REST architecture and shows how to implement GET, POST, PUT and DELETE methods. It provides directory stucture, initial environment setup and complete real world example. Technologies used: Express, MSSQL, ESLint, Nodemon.

Getting started!

To set project up:

  1. Install Node, preferably 5.0 or greater
  2. Install Git
  3. Clone the repository git clone https://github.com/blurbyte/restful-quickstart.git
  4. Install two Chrome plugins: Postman (very handy tool for API testing) and JSON Formatter (makes JSON easy to read in browser)
  5. Download and install SQL Server Management Studio (SSMS) – very helpful Azure SQL database management tool
  6. Create and set up demo Azure SQL database (it will be list of console games worth playing 😜, detailed description below)
  7. Enter project directory and install all required modules npm install
  8. Set up your Azure SQL database connection string – navigate to routes/sqlConfig.js file and enter required credinals
  9. Start demo aplication npm start -s
  10. Point browser to http://localhost:5000/api/games
  11. Check list of available demo API routes, test API with Postman (short how-to guide below), watch database changes in SSMS
  12. Take some time and review application code, first app.js file, than routes directory and finally controllers folder
  13. Create your own database and hook your own API to it 👍

Comprehensive modules overview

Module Description
express Node.js web application framework, handles routing, server requests and responses
body-parser Requests body parsing middleware
mssql MS SQL Server client, check out its detailed documentation
bluebird Promise library
nodemon Automatically restarts development server after changes
eslint, eslint-watch Reports JavaScript and React syntax errors
chalk Cool text colors and backgrounds for Terminal
npm-run-all Allows to run multiple npm scripts in parallel

Directories structure explained

List of important files and directories:

  • package.json – list of all installed npm modules
  • .npmrc – tells npm to save the exact version of the module
  • .eslintrc – ESLint configuration file, list of ESLint rules
  • .editorconfig – enforces typing rules (via code editor plugin)
  • .gitignore – tells Git which files it should ignore
  • app.js – API application starting point, Node.js server and modules setup
  • routes/sqlConfig.js – database connection string setup
  • routes/gameRouter.js – demo API routes declarations
  • controllers/gameController.js – REST verbs implementation and logic

Azure SQL database setup

  1. To created Azure SQL logical server and database for demo API follow closely steps described in this short article, don't forget to give database meaningful name such as BestGames
  2. Connect to recently created database via SQL Server Management Studio (SSMS), select it from list Databases > BestGames and open up new SQL Query window Ctrl + N
  3. To keep things simple create one new table called game by executing query F5 :
create table game(
  id integer primary key identity(1,1) not null, -- auto-increment identifier
  title nvarchar(500) not null unique,
  rating tinyint not null check(rating <= 100),  -- value constraint
  genre nvarchar(100) not null,
  descr nvarchar(max)
);
  1. Fill table with actual games data:
insert into game(title, rating, genre, descr)
values ('Dark Souls', 89, 'role-playing', 'Dark fantasy RPG...');
  1. Add as many records as you wish 😋

API Routes

List of available demo API routes with associated REST verbs

Testing RESTful API with Postman

To test GET method of our demo API (it reads records from database):

  1. Select GET from drop-down and type required url into input box, in this case http://localhost:5000/api/games/
  2. Press Send and voilà – in the section below response body and http status code will be displayed

Testing GET method with Postman for multiple elements

  1. Type http://localhost:5000/api/games/1 and check response for single item (response body and status code)

Testing GET method with Postman for single id

  1. Do the same for id which doesn't exist, for example http://localhost:5000/api/games/47

To test POST method (it creates new record in database):

  1. Select POST and type http://localhost:5000/api/games/
  2. In section just below serch box select Headers tab and provide Content-Type as a key and application/json as a value

Setting up POST method Header in Postman

  1. Select Body tab and raw option
  2. Type all required info about game in a textbox in JSON format

Setting up POST method Header in Postman

  1. After sending request check response body and status
  2. Try to send exacly same data again

To test PUT method (it updates record in database)

  1. Select PUT and type http://localhost:5000/api/games/1
  2. Further steps are exacly the same as in POST: set request header and body, check for response body and status
  3. Try again with similar request body but omit some keys and values

To test DELETE method (it deletes record from database)

  1. Select DELETE and type http://localhost:5000/api/games/1
  2. Send the request and check response status

Testing DELETE with Postman

Releases

No releases published

Packages

No packages published