Skip to content

Latest commit

 

History

History
94 lines (73 loc) · 2.24 KB

File metadata and controls

94 lines (73 loc) · 2.24 KB

Todo REST API with Node.js (No Framework)

This project is a simple Todo REST API built using Node.js core modules (http and url) without any external frameworks like Express. It demonstrates basic CRUD operations (Create, Read, Update, Delete) for managing todo items in memory.

Features

  • List all todos
  • Get a specific todo by ID
  • Add a new todo
  • Update an existing todo
  • Delete a todo

Getting Started

Prerequisites

  • Node.js installed
  • npm (usually comes with Node.js)

Installation

  1. Clone this repository or copy the project files.

  2. Navigate to the project directory:

  3. Install dependencies (if any):

    npm install
  4. Create a .env file in the project root and set the port:

    PORT=5000

Running the Server

npm run build

The server will start and listen on the port specified in your .env file.

API Endpoints

Get all todos

  • GET /todos
  • Response: Array of todo objects

Get a todo by ID

  • GET /todos/:id
  • Response: Single todo object

Add a new todo

  • POST /todos
  • Body: JSON { "text": "Task description", "completed": false }
  • Response: The created todo object

Update a todo

  • PUT /todos/:id
  • Body: JSON (fields to update, e.g. { "text": "New text", "completed": true })
  • Response: The updated todo object

Delete a todo

  • DELETE /todos/:id
  • Response: { success: true, data: {} }

Notes

  • Todos are stored in memory; data will reset when the server restarts.
  • Make sure to send valid JSON in the request body for POST and PUT requests.
  • The server returns appropriate error messages for invalid requests.

Example cURL Commands

Add a todo:

curl -X POST http://localhost:5000/todos \
  -H "Content-Type: application/json" \
  -d '{"text":"Buy groceries"}'

Get all todos:

curl http://localhost:5000/todos

Update a todo:

curl -X PUT http://localhost:5000/todos/1 \
  -H "Content-Type: application/json" \
  -d '{"completed":true}'

Delete a todo:

curl -X DELETE http://localhost:5000/todos/1

Feel free to modify and extend this project for your learning or prototyping needs!