NOTE: I have used Ubuntu 22.04 LTS and postgresql v14 for this demo
Step 0:
- Once you have an Ubuntu system follow the below steps assuming you already have git CLI installed
sudo -i // switch to root user
apt update -y
- Clone the repo:
git clone https://github.com/rohit1101/SRE-Bootcamp-Web-Server.git
- Install the dependencies required for the project:
# installs nvm (Node Version Manager)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
# download and install Node.js
nvm install 20
# verifies the right Node.js version is in the environment
node -v # should print `v20.14.0`
# verifies the right NPM version is in the environment
npm -v # should print `10.7.0`
- Install postgresql:
apt install postgresql // this install psql client for interacting with the database with quries
Switch to postgres user and use psql client
Run the following command to set
postgres
user with a valid password and exit psql client using\q
and typeexit
to logout frompostgres
user shell:
ALTER USER postgres PASSWORD 'postgres';
Step 1:
After successful pre-requisites setup following the below steps:
- Now let us clone this repo(fork and clone) and move into the src of the web server code:
git clone https://github.com/rohit1101/SRE-Bootcamp-Web-Server.git
cd SRE-Bootcamp-Web-Server
- Execute
make install
to install all the dependancies for the express js web server - Run
make db_config
command creates aknexfile.js
configuration file. - Create a new directory named migrations
mkdir migrations
- Make sure you pass the correct values for the environment variables in a new file named
.env
by referring the.env.example
. My.env
file looks as shown below.*Since I am testing the API locally I am using the default postgres DB for creating tables(on a production environment this is not recommended).
NODE_ENV=development
DB_USER=postgres
DB_PASSWORD=postgres
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=postgres
- Modify the
knexfile.js
configuration file based on your requirements
require("dotenv").config({ path: ".env" });
module.exports = {
development: {
client: "pg",
connection: {
user: process.env.DB_USER,
host: process.env.DB_HOST,
database: process.env.DB_DATABASE,
password: process.env.DB_PASSWORD,
},
pool: {
min: 2,
max: 10,
},
migrations: {
directory: "./migrations",
},
},
};
- Create a new migration, the following command will create a new migrations file in this path ->
migrations/
and update the migrations file by refering the migrations file in this repo.
make create_migrations
- This command applies the migration and creates the students table in your PostgreSQL database.
make migrate
- Now switch to
postgres
user and enterpsql
to check ourstudents
table:
Endpoints
Base URL: http://locahost:3000/v1
Health Check
GET /v1/healthcheck
- Description: Checks API health status.
- Response:
200 OK
Example Request:
curl -X GET http://localhost/v1/healthcheck
Get All Students
GET /v1/students
- Description: Retrieves all students.
- Response:
200 OK
Example Request:
curl -X GET http://localhost/v1/students
Get Student by ID
GET /v1/students/{id}
- Description: Retrieves student by ID.
- Response:
200 OK
/404 Not Found
Example Request:
curl -X GET http://your-api-domain.com/v1/students/1
Create New Student
POST /v1/students
- Description: Creates a new student.
- Response:
200 Created
/400 Bad Request
- Request body:
- name:
string
- age:
number
- department:
string
- name:
Example Request:
curl -X POST http://localhost/v1/students \
-H "Content-Type: application/json" \
-d '{"name": "Alice", "age": 23, "department": "MECH"}'
Delete Student by ID
DELETE /v1/students/{id}
- Description: Deletes student by ID.
- Response:
200 No Content
/404 Not Found
Example Request:
curl -X DELETE http://localhost/v1/students/4
Update Existing Student
PUT /v1/students/{id}
- Description: Updates student by ID.
- Response:
200 OK
/400 Bad Request
/404 Not Found
- Request body:
- name:
string
- age:
number
- department:
string
- name:
Example Request:
curl -X PUT http://localhost/v1/students/5 \
-H "Content-Type: application/json" \
-d '{"name": "Robert", "age": 24, "department": "ECE"}'
Conclusion
You now have a Node.js web server running on Ubuntu 22.04. This server can handle basic web requests and responds can be extended with more functionality as needed.
ย