Native HTTP and explicit routing
node:http creates the server. The application parses the URL and route handlers check methods and resources, including responses for unsupported paths or methods.
Selected project / 02
REST-style user API built with Node.js native HTTP module, including CRUD operations, validation, SQLite persistence and automated tests without web frameworks.
Project scope
Overview
Node HTTP Users API is a JavaScript user API built with Node.js's native HTTP module and no web framework. It brings together read and write operations, validation, and SQLite persistence.
This study project makes visible the steps a framework usually abstracts: receiving a request, interpreting its method and path, validating data, accessing storage, and composing an HTTP response.
Technical approach
The server starts the flow; the application coordinates CORS, request limiting, and errors; route handlers delegate validation and data access.
node:http creates the server. The application parses the URL and route handlers check methods and resources, including responses for unsupported paths or methods.
node:sqlite provides a synchronous connection. The repository centralizes parameterized queries for listing, reading, creating, replacing, updating, and deleting users; email has a UNIQUE constraint.
Validators and error classes keep rules out of route flow. app.js maps those errors to HTTP status codes such as 400, 409, 413, and 415.
utils/rate-limiter.js allows up to 10 requests per IP in a 60-second window and returns 429 with Retry-After. The counter lives in the process and is not shared across instances.
Architecture
Write flow: server.js → app.js → users-routes.js → validator → repository → SQLite. utils/http.js handles JSON body reading, Content-Type checks, CORS, and responses.
src/
├── server.js
├── app.js
├── routes/users-routes.js
├── validators/user-validator.js
├── repositories/user-repository.js
├── database/
│ ├── database.js
│ └── reset-database.js
├── utils/
│ ├── http.js
│ └── rate-limiter.js
└── errors/
├── validation-error.js
├── conflict-error.js
├── unsupported-media-type-error.js
├── payload-too-large-error.js
└── too-many-requests-error.js
test/
├── users-api.test.js
├── user-validator.test.js
└── rate-limiter.test.jsThe repository includes Docker Compose for the API and Caddy, plus a volume for the SQLite file. The README documents deployment on an Oracle Cloud Infrastructure VM with HTTPS through Caddy.
Demo behavior: reset-database.js clears users and inserts Portfolio Demo every five minutes. The volume persists the file, but user records are intentionally not durable.
HTTP API
The routes below are implemented in users-routes.js. Status codes show the primary success response; missing resources return 404.
GET/Simple HTML home page200GET/usersList users200GET/users/:idFind by ID200POST/usersCreate a user201PUT/users/:idReplace a user200PATCH/users/:idUpdate supplied fields200DELETE/users/:idDelete a user200CORS preflight is handled before the routes: OPTIONS returns 204. Unsupported methods return 405 with an Allow header.
Real example
POST /users
Content-Type: application/json
{
"name": "Davi",
"email": "davi@example.com",
"age": 18
}HTTP/1.1 201 Created
Content-Type: application/json
{
"id": 1,
"name": "Davi",
"email": "davi@example.com",
"age": 18
}The shape follows the README and repository return value; SQLite generates the id, so its value depends on the current data.
Validation and errors
The rules live in user-validator.js and http.js, with centralized handling in app.js.
Require the name, email, and age fields. The trimmed name must have at least two characters, email is checked with a simple regular expression, and age must be a non-negative integer.
Supports partial updates, validates supplied fields, and requires at least one recognized field among name, email, and age.
IDs must be positive integers. Bodies must be non-empty, valid JSON and no larger than 100 KB; Content-Type must be application/json.
400invalid payload, JSON, or ID404route or user not found405method not allowed · Allow409duplicate email413body over 100 KB415unsupported Content-Type429rate limit exceeded · Retry-After500unexpected errorTests
The suite uses node:test and node:assert. HTTP tests start a real server on a temporary port and send requests with fetch; data tests use an in-memory SQLite database.
test/users-api.test.jsHTTP integration: create/read, PATCH/delete, errors, CORS, and routing.10 teststest/user-validator.test.jsUnit tests for creation, required fields, email, IDs, and PATCH.6 teststest/rate-limiter.test.jsClient IP, 10-request / 60-second window, and expiry.5 testsThe HTTP suite covers POST/GET, PATCH, and DELETE; PUT is implemented but has no dedicated integration case. The limiter is tested as a function, not through an HTTP 429 response. No coverage measurement is configured.
Run locally
Use a Node.js version with node:sqlite (the project Docker image uses Node 26). The project declares no runtime npm packages, so installing dependencies is not required; npm start listens on http://localhost:3000 by default.
git clone https://github.com/davi-amaraldev/node-http-users-api.git
cd node-http-users-api
npm startLinux / macOS: npm test
PowerShell: $env:NODE_ENV='test'; node --testWhat this project demonstrates
The project demonstrates practice with methods, headers, status codes, body parsing, validation, parameterized SQL queries, and tests at different levels. Rather than relying on a ready-made CRUD abstraction, the exercise was to organize each step and explain how a request reaches stored data and returns as a response.