Express.js
Express
A Node.js web framework. Minimalist design. Middleware-based architecture.
Express
A Node.js web framework. Minimalist design. Middleware-based architecture.
Express.js is the de facto standard web framework for Node.js, released in 2010. It records more than 30 million weekly downloads on npm, and about 70% of Node.js backends use Express. With its minimal and flexible design, you can combine various middleware to build the functionality you want.
The core concept is the middleware chain. It processes requests (req) and responses (res) sequentially, and passes control to the next middleware with the next() function. Common logic such as logging, authentication, CORS, and error handling can be separated into middleware and reused.
Express 4.x has long been maintained as the stable version, while 5.x is in the alpha stage. Thanks to its large ecosystem, middleware exists for almost every feature, including passport (authentication), multer (file upload), helmet (security), and cors. Higher-level frameworks such as NestJS and Feathers are also built on Express.
As drawbacks, TypeScript support is not native, asynchronous error handling is cumbersome, and performance is lower than that of later frameworks such as Fastify. However, thanks to its overwhelming ecosystem and references, it is still the most widely used option.
// Express.js λ―Έλ€μ¨μ΄ κΈ°λ° API μλ²
import express from 'express';
import cors from 'cors';
import helmet from 'helmet';
const app = express();
// κΈλ‘λ² λ―Έλ€μ¨μ΄
app.use(helmet()); // 보μ ν€λ
app.use(cors({ origin: 'https://myapp.com' }));
app.use(express.json()); // JSON νμ±
// 컀μ€ν
λ‘κΉ
λ―Έλ€μ¨μ΄
app.use((req, res, next) => {
console.log(`${new Date().toISOString()} ${req.method} ${req.path}`);
next();
});
// μΈμ¦ λ―Έλ€μ¨μ΄
const authMiddleware = (req, res, next) => {
const token = req.headers.authorization?.split(' ')[1];
if (!token) {
return res.status(401).json({ error: 'Token required' });
}
try {
req.user = verifyToken(token);
next();
} catch {
res.status(403).json({ error: 'Invalid token' });
}
};
// λΌμ°νΈ μ μ
app.get('/api/public', (req, res) => {
res.json({ message: 'Public data' });
});
app.get('/api/protected', authMiddleware, (req, res) => {
res.json({ user: req.user, message: 'Protected data' });
});
// λΉλκΈ° λΌμ°νΈ (Express 4.xμμλ try-catch νμ)
app.get('/api/users/:id', async (req, res, next) => {
try {
const user = await db.users.findById(req.params.id);
if (!user) return res.status(404).json({ error: 'Not found' });
res.json(user);
} catch (error) {
next(error); // μλ¬ λ―Έλ€μ¨μ΄λ‘ μ λ¬
}
});
// μλ¬ μ²λ¦¬ λ―Έλ€μ¨μ΄ (4κ° μΈμ νμ)
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({ error: 'Internal server error' });
});
app.listen(3000, () => console.log('Server running on :3000'));
"We're planning to go with Express for the backend. Most of the team members have Express experience, and all the middleware we need is available, so we can build an MVP quickly. If performance becomes critical, it won't be too late to consider migrating to Fastify then."
"Interviewer: Please explain the execution order of Express middleware.
Candidate: They run in the order they were registered with app.use(). Each middleware must call next() for execution to move on to the next middleware, and if next() is not called, the chain stops. Error middleware takes 4 arguments, and when next(error) is called, it skips the regular middleware and goes to the error middleware."
"You're using await in an async route handler without try-catch, but Express 4.x doesn't automatically catch promise rejections. Use the express-async-errors package, or wrap every async handler in try-catch."
Take a mock interview on this topic β WIA SOOM AI Interview Β· Start for free