The Node.js Backend Engineer builds reliable production APIs using Express or Fastify. Node's single-threaded event loop is excellent for I/O-bound workloads (database queries, API calls, file reads) but terrible for CPU-bound work. This agent ensures async patterns are correct, errors are handled uniformly, and the server shuts down gracefully under Kubernetes pod termination.
Express vs Fastify
| Criterion | Express | Fastify |
|---|---|---|
| Throughput | ~30k req/s | ~70k req/s |
| Schema validation | Manual (Zod/Joi) | Built-in (Ajv JSON Schema) |
| TypeScript | Types available, not first-class | TypeScript-first design |
| Ecosystem | Largest - thousands of middleware | Growing - plugins, not middleware |
| Async errors | Must wrap in try/catch or use express-async-errors | Native async handler support |
Async Error Handling
// The biggest Express footgun: unhandled promise rejections crash the process
// ❌ Missing try/catch - unhandled rejection crashes app
app.get('/users/:id', async (req, res) => {
const user = await db.users.findById(req.params.id); // Can throw
res.json(user);
});
// ✅ Option 1: wrap every async route (verbose)
app.get('/users/:id', async (req, res, next) => {
try {
const user = await db.users.findById(req.params.id);
res.json(user);
} catch (err) { next(err); }
});
// ✅ Option 2: express-async-errors (installs a patch - one import)
import 'express-async-errors';
app.get('/users/:id', async (req, res) => {
const user = await db.users.findById(req.params.id);
res.json(user); // Errors forwarded to error handler automatically
});
// Global error handler - must have 4 parameters
app.use((err: Error, req: Request, res: Response, next: NextFunction) => {
console.error({ err, path: req.path, method: req.method });
const status = (err as any).status ?? 500;
res.status(status).json({
error: { code: (err as any).code ?? 'INTERNAL_ERROR', message: status === 500 ? 'Internal server error' : err.message }
});
});
Graceful Shutdown
const server = app.listen(3000);
// Handle Kubernetes SIGTERM
process.on('SIGTERM', async () => {
console.log('SIGTERM received - starting graceful shutdown');
// Stop accepting new connections
server.close(async () => {
// Finish in-flight requests, then clean up
await pool.end();
console.log('Shutdown complete');
process.exit(0);
});
// Force exit after 30s if cleanup hangs
setTimeout(() => {
console.error('Forced exit after 30s timeout');
process.exit(1);
}, 30_000);
});