Today
$2,499.00 payment received for Pro Plan subscription from Acme Corp
Sarah Johnson joined your workspace and was assigned to the Marketing team
Your January 2025 analytics report is ready. Revenue up 24% vs last month
Yesterday
New login detected from San Francisco, CA. If this wasn't you, please secure your account
New AI-powered insights are now available in your dashboard. Check out the enhanced analytics
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type
How do I build a REST API with Express.js? I need endpoints for a basic CRUD application.
I'd be happy to help you build a REST API with Express.js! Here's a complete setup for a CRUD application.
First, initialize your project and install dependencies:
mkdir my-api && cd my-api
npm init -y
npm install express cors
npm install -D nodemon
Now create your Express application with full CRUD endpoints:
const express = require('express');
const cors = require('cors');
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware
app.use(cors());
app.use(express.json());
// In-memory data store
let items = [
{ id: 1, name: 'Item One', description: 'First item' },
{ id: 2, name: 'Item Two', description: 'Second item' }
];
let nextId = 3;
// GET all items
app.get('/api/items', (req, res) => {
res.json({ success: true, data: items });
});
// GET single item
app.get('/api/items/:id', (req, res) => {
const item = items.find(i => i.id === parseInt(req.params.id));
if (!item) {
return res.status(404).json({ success: false, message: 'Item not found' });
}
res.json({ success: true, data: item });
});
// POST create item
app.post('/api/items', (req, res) => {
const { name, description } = req.body;
const newItem = { id: nextId++, name, description };
items.push(newItem);
res.status(201).json({ success: true, data: newItem });
});
// PUT update item
app.put('/api/items/:id', (req, res) => {
const item = items.find(i => i.id === parseInt(req.params.id));
if (!item) {
return res.status(404).json({ success: false, message: 'Item not found' });
}
item.name = req.body.name || item.name;
item.description = req.body.description || item.description;
res.json({ success: true, data: item });
});
// DELETE item
app.delete('/api/items/:id', (req, res) => {
const index = items.findIndex(i => i.id === parseInt(req.params.id));
if (index === -1) {
return res.status(404).json({ success: false, message: 'Item not found' });
}
items.splice(index, 1);
res.json({ success: true, message: 'Item deleted' });
});
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
This gives you the following endpoints:
GET /api/items — Retrieve all itemsGET /api/items/:id — Retrieve a single itemPOST /api/items — Create a new itemPUT /api/items/:id — Update an existing itemDELETE /api/items/:id — Delete an itemRun the server with node app.js or add "dev": "nodemon app.js" to your package.json scripts. Want me to add input validation next?
Yes, add input validation please.