All projectsProject / 02

Selected project / 02

Node HTTP API.

REST-style user API built with Node.js native HTTP module, including CRUD operations, validation, SQLite persistence and automated tests without web frameworks.

Technologies
Node.jsJavaScriptHTTPSQLite
Status

Completed

View repository

Project scope

What it covers.

  • 01Native HTTP module
  • 02User CRUD operations
  • 03REST-style routes
  • 04SQLite persistence
  • 05Validation and tests

Overview

An explicit path from request to data.

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.

Why build it without a framework?

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

Separate responsibilities in small modules.

The server starts the flow; the application coordinates CORS, request limiting, and errors; route handlers delegate validation and data access.

01

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.

02

SQLite with prepared statements

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.

03

Validation and centralized HTTP errors

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.

04

In-memory rate limiting

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

From server to SQLite, with clear boundaries.

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.js

The 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

Implemented user-resource routes.

The routes below are implemented in users-routes.js. Status codes show the primary success response; missing resources return 404.

MethodRoutePurposeSuccess
GET/Simple HTML home page200
GET/usersList users200
GET/users/:idFind by ID200
POST/usersCreate a user201
PUT/users/:idReplace a user200
PATCH/users/:idUpdate supplied fields200
DELETE/users/:idDelete a user200

CORS preflight is handled before the routes: OPTIONS returns 204. Unsupported methods return 405 with an Allow header.

Real example

Create a user with JSON.

Request

POST /users
Content-Type: application/json

{
  "name": "Davi",
  "email": "davi@example.com",
  "age": 18
}

Response · 201 Created

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

Straightforward rules mapped to HTTP statuses.

The rules live in user-validator.js and http.js, with centralized handling in app.js.

POST and PUT

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.

PATCH

Supports partial updates, validates supplied fields, and requires at least one recognized field among name, email, and age.

ID and body

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 ID
404route or user not found
405method not allowed · Allow
409duplicate email
413body over 100 KB
415unsupported Content-Type
429rate limit exceeded · Retry-After
500unexpected error

Tests

21 native tests across three files.

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 tests
test/user-validator.test.jsUnit tests for creation, required fields, email, IDs, and PATCH.6 tests
test/rate-limiter.test.jsClient IP, 10-request / 60-second window, and expiry.5 tests

The 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

No external npm dependencies.

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 start

Run the tests

Linux / macOS: npm test
PowerShell: $env:NODE_ENV='test'; node --test

What this project demonstrates

Tracing the path from HTTP to persistence.

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.

Next projectJava Backend 90d