What You’ll Learn in This Tutorial
✓ Express project setup
✓ Routing and HTTP methods
✓ Middleware concepts
✓ Request/Response handling
✓ Error handling
✓ Validation and security
Prerequisites
- Node.js 18 or higher installed
- Basic JavaScript knowledge
Step 1: Project Setup
mkdir express-api
cd express-api
npm init -y
npm install express cors helmet
npm install -D typescript @types/node @types/express ts-node nodemon
tsconfig.json
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"outDir": "./dist"
},
"include": ["src/**/*"]
}
package.json scripts
{
"scripts": {
"dev": "nodemon --exec ts-node src/index.ts",
"build": "tsc",
"start": "node dist/index.js"
}
}
Step 2: Basic Server
// src/index.ts
import express from 'express';
import cors from 'cors';
import helmet from 'helmet';
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware
app.use(helmet());
app.use(cors());
app.use(express.json());
// Routes
app.get('/', (req, res) => {
res.json({ message: 'Hello, Express!' });
});
// Start server
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});
Step 3: Routing
// src/routes/users.ts
import { Router } from 'express';
const router = Router();
interface User {
id: number;
name: string;
email: string;
}
let users: User[] = [
{ id: 1, name: 'John Doe', email: 'john@example.com' },
{ id: 2, name: 'Jane Smith', email: 'jane@example.com' },
];
// Get list
router.get('/', (req, res) => {
res.json(users);
});
// Get detail
router.get('/:id', (req, res) => {
const user = users.find(u => u.id === parseInt(req.params.id));
if (!user) {
return res.status(404).json({ error: 'User not found' });
}
res.json(user);
});
// Create
router.post('/', (req, res) => {
const { name, email } = req.body;
const newUser: User = {
id: users.length + 1,
name,
email,
};
users.push(newUser);
res.status(201).json(newUser);
});
// Update
router.put('/:id', (req, res) => {
const index = users.findIndex(u => u.id === parseInt(req.params.id));
if (index === -1) {
return res.status(404).json({ error: 'User not found' });
}
users[index] = { ...users[index], ...req.body };
res.json(users[index]);
});
// Delete
router.delete('/:id', (req, res) => {
const index = users.findIndex(u => u.id === parseInt(req.params.id));
if (index === -1) {
return res.status(404).json({ error: 'User not found' });
}
users.splice(index, 1);
res.status(204).send();
});
export default router;
Step 4: Middleware
// src/middleware/logger.ts
import { Request, Response, NextFunction } from 'express';
export function logger(req: Request, res: Response, next: NextFunction) {
const start = Date.now();
res.on('finish', () => {
const duration = Date.now() - start;
console.log(`${req.method} ${req.path} ${res.statusCode} - ${duration}ms`);
});
next();
}
// src/middleware/auth.ts
export function authenticate(req: Request, res: Response, next: NextFunction) {
const token = req.headers.authorization?.split(' ')[1];
if (!token) {
return res.status(401).json({ error: 'Unauthorized' });
}
// Token verification logic
next();
}
Step 5: Error Handling
// src/middleware/errorHandler.ts
import { Request, Response, NextFunction } from 'express';
export class AppError extends Error {
constructor(public statusCode: number, message: string) {
super(message);
}
}
export function errorHandler(
err: Error,
req: Request,
res: Response,
next: NextFunction
) {
if (err instanceof AppError) {
return res.status(err.statusCode).json({ error: err.message });
}
console.error(err);
res.status(500).json({ error: 'Internal Server Error' });
}
Step 6: Application Integration
// src/index.ts
import express from 'express';
import cors from 'cors';
import helmet from 'helmet';
import userRoutes from './routes/users';
import { logger } from './middleware/logger';
import { errorHandler } from './middleware/errorHandler';
const app = express();
app.use(helmet());
app.use(cors());
app.use(express.json());
app.use(logger);
app.use('/api/users', userRoutes);
app.use(errorHandler);
app.listen(3000, () => {
console.log('Server running on http://localhost:3000');
});
Summary
Express is a lightweight and flexible Node.js framework. By understanding the middleware pattern and implementing proper error handling, you can build robust APIs.
← Back to list