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.
To test this application, you need Docker, NodeJS, and the Azure Functions Core Tools npm package on major version 4 installed globally.
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
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 theValues
object, this file should have this schema. - Standalone:
.env
, with key-value pairs following separated by=
.
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"
Variables available in the .env
file:
PORT=3000
DB_PORT=5432
DB_USER="postgres"
DB_PASSWORD="postgres"
DB_DATABASE="postgres"
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"
}
}
After setting up the environment, you can start one of the APIs or both:
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.
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.
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"
}
]
Request
GET /user/1 HTTP/1.1
Host: localhost:3000
Expected Response Body
{
"id": 1,
"username": "admin",
"name": "Administrator",
"license": "Pro"
}
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
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
Request
DELETE /user/1 HTTP/1.1
Host: localhost:3000
Expected Response Status Code
204 No Content
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
}
]
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.
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.
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"
}
]
Request
GET /api/getUser?id=1 HTTP/1.1
Host: localhost:7071
Expected Response Body
{
"id": 1,
"username": "admin",
"name": "Administrator",
"license": "Pro"
}
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
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
Request
DELETE /api/deleteUser?id=1 HTTP/1.1
Host: localhost:7071
Expected Response Status Code
204 No Content
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
}
]