A simple, scalable Node.js web application demonstrating how to handle multiple concurrent connections using Node.js core modules. This API allows users to perform basic CRUD operations (currently GET and POST) on a list of products stored in a JSON file.
- Features
- Tech Stack
- How to Run
- API Endpoints
- Scalability Explanation
- Example Test with Postman
- Future Improvements
- View all products (
GET /products) - Add new products (
POST /products) - Update a product (
PUT /products?id=ID) - Delete a product (
DELETE /products?id=ID) - Handles multiple connections concurrently using non-blocking I/O
- Reads from and writes to
products.jsonfile - No external frameworks (built entirely with Node.js core modules)
- Runtime: Node.js
- Core Modules:
http,fs,path,url
-
Clone the repository or download the code.
-
Open the project folder in VSCode.
-
Make sure your
products.jsonfile is in adata/folder inside your project like this:project-folder/ ├── data/ │ └── products.json ├── index.js -
Run the server:
node index.js
-
Make requests using Postman or Thunder Client at:
http://localhost:500/products
Returns a welcome message.
Response:
{ "message": "Welcome to Shurah Product API!" }Returns the full list of products from products.json.
Example Response:
[
{
"id": 0,
"name": "Coke",
"category": "Drinks",
"quantity": 10
},
{
"id": 1,
"name": "Bournvita",
"category": "Provision",
"quantity": 20
}
]Adds a new product to the list.
Request Body (JSON):
{
"name": "Milo",
"category": "Provision",
"quantity": 12
}Success Response:
Product added successfully
Updates an existing product with the given id.
Request Body (JSON):
{
"name": "Milo",
"category": "Provision",
"quantity": 15
}Success Response:
Product updated successfully
Deletes the product with the specified id.
Success Response:
Product deleted successfully
This project showcases Node.js’s non-blocking I/O and asynchronous event loop capabilities.
- When multiple users make requests (e.g., sending new product data), Node.js doesn't block other operations — it reads and writes files asynchronously.
- This means even under multiple concurrent connections, the server remains responsive.
- Node’s single-threaded nature, combined with its event-driven architecture, makes it highly suitable for I/O-heavy tasks like this one.
- Download and install Postman or use the Thunder Client extension in VSCode.
- Set method to
POST. - Set the URL to:
http://localhost:500/products - Under Body > raw > JSON, paste this:
{
"name": "Pepsi",
"category": "Drinks",
"quantity": 5
}- Click Send.
- Add validation for product fields.
- Handle error responses with more clarity and consistency.
- Use
streammodule for large file handling. - Persist data with a database like MongoDB or PostgreSQL.