A one-file todo API with Fastify and PostgreSQL. Has basic stuff like rate limiting, CORS, and handles shutdowns properly. Nothing fancy - it just works.
P.S. Yes, it's all in one file. No, we're not sorry.
GET /health
- Health check endpointGET /todos
- Retrieve all todosPOST /todos
- Create a new todoPUT /todos/:id
- Update an existing todoDELETE /todos/:id
- Delete a todo
- Node.js (v20 or higher)
- PostgreSQL database
- npm or yarn
Create a .env
file in the root directory with the following variables:
NODE_ENV=development
PORT=3000
PGUSER=your_db_user
PGHOST=your_db_host
PGDATABASE=your_db_name
PGPASSWORD=your_db_password
PGPORT=5432
CLIENT_URL=http://localhost:3000
# Install dependencies
npm install
# Start the server
npm start
Ensure your PostgreSQL database has the following table:
CREATE TABLE todos (
id SERIAL PRIMARY KEY,
title TEXT NOT NULL,
completed BOOLEAN DEFAULT FALSE
);
- Rate limiting is automatically enabled in production
- SSL is enforced for database connections
- CORS is restricted to the specified CLIENT_URL
- Logging is set to 'info' level with minimal request details