A simple RESTful Todo API built with TypeScript and Express. This application provides endpoints for creating, reading, updating, and deleting Todo items. For simplicity, it uses an in-memory store (an array) to keep track of todos.
- Overview
- Features
- Project Structure
- Tech Stack
- Installation
- Configuration
- Usage
- Development
- Build & Deployment
- Testing
- Troubleshooting
- Contributing
- License
This project is a minimal Todo API written in a single TypeScript file (app.ts
). It uses Express for handling HTTP requests, body-parser for parsing JSON request bodies, and uuid for generating unique identifiers. The API supports full CRUD (Create, Read, Update, Delete) operations on Todo items.
- CRUD Operations: Create, read, update, and delete todo items.
- In-Memory Data Store: Stores todo items in an array for simplicity.
- Type Safety: Written in TypeScript for type safety and improved code quality.
- RESTful API: Follows REST principles with clear endpoints and HTTP status codes.
- Lightweight: Minimal dependencies and setup for quick development.
todo-api/
├── app.ts # Main application file with the full API implementation
├── package.json # Project metadata and dependencies
├── tsconfig.json # TypeScript compiler configuration
└── .gitignore # Files/folders to ignore in Git
- Language: TypeScript
- Runtime: Node.js
- Framework: Express
- Libraries: body-parser, uuid
- Build Tool: ts-node (for development) and tsc (for compiling)
-
Clone the Repository
git clone https://github.com/your-username/todo-api.git cd todo-api
-
Install Dependencies
Make sure you have Node.js installed. Then run:
npm install
This project uses default settings and does not require additional configuration. The server runs on port 3000 by default. You can change the port by setting the PORT
environment variable.
For development, you can use ts-node
to run the app without compiling:
npm start
This will start the server on http://localhost:3000.
The following endpoints are available:
-
GET
/todos
Retrieve all todo items.
Response:[ { "id": "550e8400-e29b-41d4-a716-446655440000", "title": "Buy groceries", "completed": false } ]
-
GET
/todos/:id
Retrieve a single todo item by its ID.
Response:{ "id": "550e8400-e29b-41d4-a716-446655440000", "title": "Buy groceries", "completed": false }
-
POST
/todos
Create a new todo item.
Request Body:{ "title": "Buy groceries" }
Response:
{ "id": "newly-generated-uuid", "title": "Buy groceries", "completed": false }
-
PUT
/todos/:id
Update an existing todo item.
Request Body Example:{ "title": "Buy groceries and cook dinner", "completed": true }
Response:
{ "id": "550e8400-e29b-41d4-a716-446655440000", "title": "Buy groceries and cook dinner", "completed": true }
-
DELETE
/todos/:id
Delete a todo item by its ID.
Response:{ "id": "550e8400-e29b-41d4-a716-446655440000", "title": "Buy groceries and cook dinner", "completed": true }
For local development, use ts-node
to run the TypeScript application without compiling:
npm start
If you prefer compiling to JavaScript, run:
npm run build
Then execute the compiled JavaScript from the dist
folder:
node dist/app.js
-
Build:
The commandnpm run build
uses the TypeScript compiler (tsc
) to compile the source code into thedist
directory. -
Deployment:
To deploy the app, compile it withnpm run build
and deploy the contents of thedist
folder to your Node.js hosting environment. Ensure that you set the environment variablePORT
if you need a custom port.
You can use tools like Postman or curl
to test the API endpoints. Automated testing can be added using frameworks such as Mocha or Jest if required.
-
Server Not Starting:
Ensure that all dependencies are installed and that you are running the correct Node.js version. -
Endpoint Not Found:
Double-check the URL and port number. Ensure the server is running on the expected port. -
TypeScript Errors:
Review the error messages and adjust your code ortsconfig.json
settings accordingly.
Contributions are welcome! To contribute:
- Fork the repository.
- Create a feature branch (e.g.,
git checkout -b feature/my-new-feature
). - Commit your changes with clear commit messages.
- Push your branch to your fork and create a pull request.
- Follow any additional guidelines provided in the repository.
This project is licensed under the MIT License. See the LICENSE file for details.
Enjoy using the Todo API, and feel free to extend it with persistent storage, authentication, and more advanced features!