X Tutup
Skip to content

shongeorg/node-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

24 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Node.js REST API

A modern Vanilla Node.js REST API with CLI, Interactive UI Tester, and comprehensive testing suite.

Node.js Prisma PostgreSQL Tests

✨ Features

  • πŸ›  CLI Tool - Laravel/AdonisJS-style commands for routes and scaffolding
  • 🌐 UI API Tester - Interactive web interface for testing endpoints
  • πŸ§ͺ Testing Framework - Custom test runner on pure Node.js (31 tests)
  • πŸ“¦ Service Architecture - Clean separation of business logic and HTTP handlers
  • πŸš€ Prisma ORM - Type-safe database access with PostgreSQL
  • ⚑ Graceful Shutdown - Proper signal handling and cleanup

πŸš€ Quick Start

Prerequisites

  • Node.js v24.14.0 or higher
  • PostgreSQL database (or use Prisma Accelerate)

Installation

# Clone the repository
git clone <your-repo-url>
cd node-api

# Install dependencies
npm install

# Set up environment variables
cp .env.example .env
# Edit .env with your DATABASE_URL

# Generate Prisma Client
npx prisma generate

# Run migrations
npx prisma migrate dev

# Start development server
npm run dev

Open UI Tester

Navigate to http://localhost:3000 in your browser.

πŸ“‘ API Endpoints

Index & Utility

Method Endpoint Description
GET / UI API Tester
GET /faker Generate fake user data

Posts (Static Data)

Method Endpoint Description
GET /posts Get all posts
GET /posts/:id Get post by ID

Users (Static Data)

Method Endpoint Description
GET /users Get all users
GET /users/:id Get user by ID

Categories (Database)

Method Endpoint Description
GET /categories Get all categories
GET /categories/:id Get category by ID
POST /categories Create category
PUT /categories/:id Update category
DELETE /categories/:id Delete category

Notes (Database)

Method Endpoint Description
GET /notes Get all notes
GET /notes/:id Get note by ID
POST /notes Create note
PUT /notes/:id Update note
DELETE /notes/:id Delete note

πŸ›  CLI Commands

Show All Routes

node src/cli.js routes

Output:

β—Ό API Routes
β”Œβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Method β”‚ Path            β”‚ Handler              β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ GET    β”‚ /categories     β”‚ categoriesController β”‚
β”‚ POST   β”‚ /categories     β”‚ categoriesController β”‚
β”‚ PUT    β”‚ /categories/:id β”‚ categoriesController β”‚
β”‚ DELETE β”‚ /categories/:id β”‚ categoriesController β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Generate CRUD (Scaffold)

node src/cli.js scaffold <EntityName>

# Examples
node src/cli.js scaffold Product
node src/cli.js scaffold Order
node src/cli.js scaffold Customer

This creates:

  • src/services/<entity>.js - Service layer with CRUD methods
  • src/controllers/<entity>.js - HTTP request handlers
  • Auto-registers 5 routes in src/routes.js

Show Help

node src/cli.js help

πŸ§ͺ Testing

Run Tests

npm test

Test Output

βœ“ Passed: 31
βœ— Failed: 0
Total: 31

Test Structure

Tests are located in tests/unit/ and use a custom test runner built on pure Node.js (node:assert, node:events).

import { describe, it, assert, beforeEach } from '../runner.js';
import { mockPrisma } from '../mocks/prisma.js';

describe('CategoriesController', () => {
  beforeEach(() => {
    mockPrisma.$reset();
  });

  it('should return all categories', async () => {
    const controller = createController();
    const result = await controller.getAll();
    assert(Array.isArray(JSON.parse(result)));
  });
});

πŸ—„ Database Schema

Category Model

model Category {
  id    Int    @id @default(autoincrement())
  name  String
  notes Note[]
}

Note Model

model Note {
  id         Int      @id @default(autoincrement())
  title      String
  content    String?
  archived   Boolean  @default(false)
  categoryId Int
  category   Category @relation(fields: [categoryId], references: [id])
}

πŸ“ Project Structure

node-api/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ server.js           # HTTP server + static files
β”‚   β”œβ”€β”€ config.js           # Environment configuration
β”‚   β”œβ”€β”€ cli.js              # CLI tool
β”‚   β”œβ”€β”€ routes.js           # Route definitions
β”‚   β”œβ”€β”€ loger.js            # Logging middleware
β”‚   β”œβ”€β”€ controllers/        # HTTP handlers
β”‚   β”‚   β”œβ”€β”€ categories.js
β”‚   β”‚   β”œβ”€β”€ notes.js
β”‚   β”‚   └── index.js
β”‚   └── services/           # Business logic
β”‚       β”œβ”€β”€ category.js
β”‚       └── note.js
β”œβ”€β”€ tests/
β”‚   β”œβ”€β”€ runner.js           # Test runner
β”‚   β”œβ”€β”€ mocks/
β”‚   β”‚   └── prisma.js       # Mock Prisma client
β”‚   └── unit/
β”‚       β”œβ”€β”€ categories.test.js
β”‚       └── notes.test.js
β”œβ”€β”€ public/
β”‚   └── index.html          # UI API Tester
β”œβ”€β”€ prisma/
β”‚   β”œβ”€β”€ schema.prisma
β”‚   β”œβ”€β”€ migrations/
β”‚   └── seeds.js
β”œβ”€β”€ .env.example
β”œβ”€β”€ package.json
└── README.md

βš™οΈ Configuration

Environment Variables

Create a .env file in the root directory:

# Database (Prisma Accelerate or local PostgreSQL)
DATABASE_URL="prisma+postgres://accelerate.prisma-data.net/?api_key=YOUR_KEY"
# Or: DATABASE_URL="postgresql://user:password@localhost:5432/nodeapi"

# Server
PORT=3000
NODE_ENV=development

πŸ“œ Available Scripts

npm start           # Start production server
npm run dev         # Start development server with --watch
npm test            # Run unit tests
npm run seeds       # Seed database with test data
node src/cli.js routes          # Show all routes
node src/cli.js scaffold Product  # Generate CRUD
node src/cli.js help            # Show CLI help

🎯 Usage Examples

Using UI Tester

  1. Open http://localhost:3000
  2. Select an endpoint from the left panel
  3. Configure request (ID, JSON body)
  4. Click "Send Request"
  5. View formatted response

Using cURL

# Get all categories
curl http://localhost:3000/categories

# Get category by ID
curl http://localhost:3000/categories/1

# Create category
curl -X POST http://localhost:3000/categories \
  -H "Content-Type: application/json" \
  -d '{"name": "New Category"}'

# Update category
curl -X PUT http://localhost:3000/categories/1 \
  -H "Content-Type: application/json" \
  -d '{"name": "Updated Category"}'

# Delete category
curl -X DELETE http://localhost:3000/categories/1

Using PowerShell

# Get all notes
Invoke-RestMethod -Uri 'http://localhost:3000/notes'

# Create note
$body = @{title='New Note'; categoryId=1} | ConvertTo-Json
Invoke-RestMethod -Uri 'http://localhost:3000/notes' `
  -Method Post -Body $body -ContentType 'application/json'

πŸ— Architecture

Request Flow

HTTP Request
    ↓
server.js (Static files β†’ API routing)
    ↓
routes.js (Route matching)
    ↓
controller (HTTP handling, validation)
    ↓
service (Business logic, Prisma)
    ↓
Prisma β†’ Database
    ↓
Response (JSON)

Key Design Decisions

  • ES Modules - Modern JavaScript module system
  • Service Layer - Isolated business logic for easy testing
  • Controller Layer - HTTP-specific handling and validation
  • Mock Prisma - In-memory database for unit tests
  • Graceful Shutdown - Proper cleanup on SIGTERM/SIGINT

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the ISC License.

πŸ™ Acknowledgments

  • CLI design inspired by Laravel Artisan and AdonisJS Ace
  • UI Tester design inspired by modern API testing tools
  • Testing framework built on Node.js built-in modules

Built with ❀️ using Node.js, Prisma, and PostgreSQL

About

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

Β 
Β 
Β 

Contributors

X Tutup