What you'll build
Your first backend server with Node.js and Express. You'll create a complete REST API with endpoints to list, create, update, and delete tasks (CRUD). You'll learn how HTTP requests work (GET, POST, PUT, DELETE) and how the frontend communicates with the backend. By the end, you'll have a server running on your computer that responds to requests like a real API.
Step 1: Ask an AI for the code
I need a REST API with Express and Node.js that:
- Has CRUD endpoints for tasks (todos)
- Uses JSON for requests and responses
- Saves in memory (array)
- Has CORS enabled
- Port 3001
Give me the complete code.
Step 2: Create the project
mkdir todo-api
cd todo-api
npm init -y
npm install express cors
Paste the code the AI gave you in server.js
Step 3: Run it
node server.js
Step 4: Test with curl
# List all
curl http://localhost:3001/todos
# Create one
curl -X POST http://localhost:3001/todos \
-H "Content-Type: application/json" \
-d '{"text": "My first task"}'
# Update
curl -X PUT http://localhost:3001/todos/1 \
-H "Content-Type: application/json" \
-d '{"completed": true}'
# Delete
curl -X DELETE http://localhost:3001/todos/1
Typical code
const express = require('express')
const cors = require('cors')
const app = express()
app.use(cors())
app.use(express.json())
let todos = []
let nextId = 1
app.get('/todos', (req, res) => {
res.json(todos)
})
app.post('/todos', (req, res) => {
const todo = { id: nextId++, ...req.body, completed: false }
todos.push(todo)
res.status(201).json(todo)
})
app.listen(3001, () => console.log('API on port 3001'))
HTTP Methods
| Method | Usage |
|---|---|
| GET | Read data |
| POST | Create new |
| PUT | Update existing |
| DELETE | Delete |
If something failed
| Error | Cause | Solution |
|---|---|---|
EADDRINUSE | Port in use | Change the port |
Cannot find module | Missing dependency | npm install |
| CORS error | CORS not enabled | Add app.use(cors()) |
Next step
โ CLI Tool with Python โ Automate with Python