Create REST APIs with Node.js, Express, Jest, and Supertest...
Automate the creation of production-ready REST APIs with Node.js, Express, Jest, and Supertest following industry best practices.
Follow these steps in order:
Run the initialization script to set up the project:
bash scripts/init_project.sh <project-name>
This creates the project directory, initializes npm, installs dependencies (express, dotenv, jest, supertest), and configures package.json with test scripts.
Gather requirements from the user and create a structured API specification. Use the format defined in references/api-spec-format.md.
Ask the user:
Create a specification document (JSON or markdown table) that defines:
Save the specification to <project-name>/api-spec.json or <project-name>/api-spec.md.
Create the standard project structure:
cd <project-name>
mkdir -p src/routes src/controllers src/middleware tests
Copy template files:
# Core application files
cp templates/app-template.js src/app.js
cp templates/server-template.js src/server.js
# Middleware
cp templates/error-handler.js src/middleware/errorHandler.js
cp templates/validation-middleware.js src/middleware/validation.js
# Configuration files
cp templates/.env.example .env.example
cp templates/.gitignore .gitignore
Create a .env file from .env.example with default values.
For each endpoint in the specification, generate:
Route file (src/routes/<resource>.js):
Controller file (src/controllers/<resource>Controller.js):
Update app.js:
/api/resource)Refer to references/best-practices.md for:
For endpoints with request bodies, create validation schemas using the validation middleware:
const createSchema = {
title: {
type: 'string',
required: true,
minLength: 1,
maxLength: 200
},
description: {
type: 'string',
required: false
}
};
Apply validation in routes:
router.post('/', validateBody(createSchema), controller.create);
For each endpoint, generate test files in tests/ directory following the patterns in references/testing-patterns.md.
Create tests for:
Success Cases:
Failure Cases:
Edge Cases:
Use the test template structure:
const request = require('supertest');
const app = require('../src/app');
describe('Resource API', () => {
describe('GET /api/resource', () => {
// Tests here
});
describe('POST /api/resource', () => {
// Tests here
});
// ... other methods
});
Execute the test suite:
cd <project-name>
npm test
Review test results:
Run tests in watch mode during development:
npm run test:watch
The generated project follows this structure:
<project-name>/
โโโ src/
โ โโโ app.js # Express app setup with middleware
โ โโโ server.js # Server entry point
โ โโโ routes/ # Route definitions
โ โ โโโ <resource>.js
โ โโโ controllers/ # Business logic
โ โ โโโ <resource>Controller.js
โ โโโ middleware/ # Custom middleware
โ โ โโโ errorHandler.js # Error handling utilities
โ โ โโโ validation.js # Request validation
โ โโโ models/ # Data models (optional)
โโโ tests/ # Test files
โ โโโ <resource>.test.js
โโโ .env # Environment variables
โโโ .env.example # Environment template
โโโ .gitignore # Git ignore rules
โโโ package.json # Dependencies and scripts
โโโ api-spec.json # API specification
Consult these references for detailed guidance:
references/api-spec-format.mdreferences/best-practices.mdrest-api-nodejs/references/testing-patterns.mdKey principles:
Start the development server:
npm run dev
Start the production server:
npm start
The API will be available at http://localhost:3000 (or the port specified in .env).
Run all tests:
npm test
Run tests in watch mode:
npm run test:watch
After generating the API: