Skip to content

Simple demonstration of NodeJS API in Express and Azure Functions

License

Notifications You must be signed in to change notification settings

Giancarl021/API-Demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

API-Demo

Simple demonstration of NodeJS API in Express and Azure Functions

This repository does NOT includes deployment scripts and is for learning and testing purposes only.

Environment

To test this application, you need Docker, NodeJS, and the Azure Functions Core Tools npm package on major version 4 installed globally.

Database

Before running any of the APIs, you will need to run the up.js script to create and migrate a PostgreSQL database container.

In order to do that, run the following commands from the root directory of this repository:

cd environment
npm install
npm run up

After the command succeeds, the postgres database should be available on the port 5432.

If you want to stop the database you can run the down.js script to remove the database container and delete the volumes created.

Keep in mind that any data in the database will be lost if you run this script.

In order to do that, run the following commands from the root directory of this repository:

cd environment
npm run down

Environment Variables

If you want to change the database credentials, port, or other application settings, you can set environment variables in the following files:

  • Environment: .env, with key-value pairs following separated by =.
  • Azure Functions: local.settings.json on the Values object, this file should have this schema.
  • Standalone: .env, with key-value pairs following separated by =.

Environment

Variables available in the .env file:

CONTAINER_NAME="postgres"
CONTAINER_IMAGE="postgres:latest"
CONTAINER_PORT=5432
POSTGRES_USER="postgres"
POSTGRES_PASSWORD="postgres"
POSTGRES_DATABASE="postgres"

Standalone

Variables available in the .env file:

PORT=3000
DB_PORT=5432
DB_USER="postgres"
DB_PASSWORD="postgres"
DB_DATABASE="postgres"

Azure Functions

Variables available in the local.settings.json file:

{
    "IsEncrypted": false,
    "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "FUNCTIONS_WORKER_RUNTIME": "node",
  
        "DB_PORT": 5432,
        "DB_USER": "postgres",
        "DB_PASSWORD": "postgres",
        "DB_DATABASE": "postgres"
    }
}

Usage

After setting up the environment, you can start one of the APIs or both:

Standalone

This API is made using NodeJS and the Express API framework. It can be deployed in any environment with NodeJS installed or even dockerized.

This API is to show an example of an platform-agnostic backend project in NodeJS.

To run this API, run the following command from the root directory of this repository:

cd standalone
npm install
npm start

This will start the API on port 3000, or the PORT environment variable.

Endpoints

This API is RESTful, you can call the following endpoints:

These example responses are considering that you have NOT changed the database initial state and are calling the endpoints in order.

Query all users

Request

GET /user HTTP/1.1
Host: localhost:3000

Expected Response Body

[
    {
        "id": 1,
        "username": "admin",
        "name": "Administrator",
        "license": "Pro"
    },
    {
        "id": 2,
        "username": "the_master_j0hn",
        "name": "John",
        "license": "Basic"
    },
    {
        "id": 3,
        "username": "the_p00r_jane",
        "name": "Jane",
        "license": "Free"
    }
]
Get a user by id

Request

GET /user/1 HTTP/1.1
Host: localhost:3000

Expected Response Body

{
    "id": 1,
    "username": "admin",
    "name": "Administrator",
    "license": "Pro"
}
Create a user

Request

POST /user HTTP/1.1
Host: localhost:3000
Content-Type: application/json
Content-Length: 88

{
    "name": "Giancarlo Luz",
    "username": "Giancarl021",
    "licenseType": 3
}

Expected Response Status Code

201 Created
Update a user

Request

PUT /user/1 HTTP/1.1
Host: localhost:3000
Content-Type: application/json
Content-Length: 95

{
    "name": "Master Administrator",
    "username": "master-adm",
    "licenseType": 3
}

Expected Response Status Code

204 No Content
Delete a user

Request

DELETE /user/1 HTTP/1.1
Host: localhost:3000

Expected Response Status Code

204 No Content
Query all licenses

Request

GET /license HTTP/1.1
Host: localhost:3000

Expected Response Body

[
    {
        "id": 1,
        "name": "Free",
        "price": 0
    },
    {
        "id": 2,
        "name": "Basic",
        "price": 9.99
    },
    {
        "id": 3,
        "name": "Pro",
        "price": 19.99
    }
]

Azure Functions

This API is made using NodeJS and the Azure Functions Core Tools scaffolding. It can be deployed only on Azure and be dockerized if a specific environment is needed.

To run this API, run the following command from the root directory of this repository:

cd azure-functions
npm install
npm start

This will start the API on port 7071.

Endpoints

This API is RESTful, you can call the following endpoints:

These example responses are considering that you have NOT changed the database initial state and are calling the endpoints in order.

Query all users

Request

GET /api/listUsers HTTP/1.1
Host: localhost:7071

Expected Response Body

[
    {
        "id": 1,
        "username": "admin",
        "name": "Administrator",
        "license": "Pro"
    },
    {
        "id": 2,
        "username": "the_master_j0hn",
        "name": "John",
        "license": "Basic"
    },
    {
        "id": 3,
        "username": "the_p00r_jane",
        "name": "Jane",
        "license": "Free"
    }
]
Get a user by id

Request

GET /api/getUser?id=1 HTTP/1.1
Host: localhost:7071

Expected Response Body

{
    "id": 1,
    "username": "admin",
    "name": "Administrator",
    "license": "Pro"
}
Create a user

Request

POST /api/createUser HTTP/1.1
Host: localhost:7071
Content-Type: application/json
Content-Length: 88

{
    "name": "Giancarlo Luz",
    "username": "Giancarl021",
    "licenseType": 3
}

Expected Response Status Code

201 Created
Update a user

Request

PUT /api/editUser?id=1 HTTP/1.1
Host: localhost:7071
Content-Type: application/json
Content-Length: 95

{
    "name": "Master Administrator",
    "username": "master-adm",
    "licenseType": 3
}

Expected Response Status Code

204 No Content
Delete a user

Request

DELETE /api/deleteUser?id=1 HTTP/1.1
Host: localhost:7071

Expected Response Status Code

204 No Content
Query all licenses

Request

GET /api/listLicenses HTTP/1.1
Host: localhost:7071

Expected Response Body

[
    {
        "id": 1,
        "name": "Free",
        "price": 0
    },
    {
        "id": 2,
        "name": "Basic",
        "price": 9.99
    },
    {
        "id": 3,
        "name": "Pro",
        "price": 19.99
    }
]

About

Simple demonstration of NodeJS API in Express and Azure Functions

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published