Skip to content

A basic REST API implemented in NodeJS and documented with Swagger that uses JWT authentication

License

Notifications You must be signed in to change notification settings

cmartinezal/Basic-REST-API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 

Repository files navigation

Basic-REST-API

Table of Contents
  1. About The Project
  2. Project Achitecture
  3. Getting Started
  4. Usage
  5. Example: Get all users

About The Project

This project creates basic REST API to manage users implemented in NodeJS and documented with Swagger that uses JWT authentication.

To simplify the project, the implementation has been done from the server side without using any database.

A JSON file is used to save data and mock up the database layer, although this logic can be transferred to other databases.

Project Achitecture

This project uses a 3 layer architecture:

Project architecture

  • 1. Controller: Handles the requests and responses for our endpoints.
  • 2. Service Layer: Defines business logic and exports the methods that are used by the controller.
  • 3. Data Access Layer: This is the layer that interacts with the Database or in our case the local JSON file that mimics our Database.

Getting Started

Prerequisites

npm must be installed.
To download the latest version of npm, on the command line, run the following command:

    npm install -g npm@latest

Installation

To install the project we have to install npm and execute the following commands:

    npm install
    npm start

Usage

Swagger Documentation

Swagger documentation

  • Swagger Web: http://localhost:3000/api/docs

  • JSON: http://localhost:3000/api/docs.json

Authorization Endpoints

The authorization is based in OAuth 2.0.
The access token is required to access to users secured enpoints.
The refresh token exists to enable authorization servers to use short lifetimes for access tokens without needing to involve the user when the token expires.

Authorization Endpoints

The requests can be performed to the following urls:

  • POST http://localhost:3000/api/v1/auth/token
  • POST http://localhost:3000/api/v1/auth/refresh

REST API Endpoints

User Endpoints

The requests can be performed to the following urls:

  • GET http://localhost:3000/api/users
  • GET http://localhost:3000/api/users/:userId
  • POST http://localhost:3000/api/users
  • PUT http://localhost:3000/api/users/:userId
  • DELETE http://localhost:3000/api/users/:userId

Example: Get all users

1. Get token with user data

Use endpoint POST api/v1/auth/token. You can use this test user to authorize:

  • email: bettysloan@email.com
  • password: password1

A JWT will be issued for the test user specified:

Token request

Copy the value of access_token in Authorization to allow Swagger to send the Authorization header required in the secured enpoints:

Authorization with token

2. Access to Get all users secured endpoint

As we can see the authorization header is sent in the request and we can access to the secure endpoint:

Get all users example

About

A basic REST API implemented in NodeJS and documented with Swagger that uses JWT authentication

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published