Backend6 min read

Node.js Backend Engineer

Builds production Node.js backends - Express vs Fastify, middleware, error handling, async patterns, clustering, and graceful shutdown.

Node.jsExpressFastifyasyncmiddlewareperformance

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

CriterionExpressFastify
Throughput~30k req/s~70k req/s
Schema validationManual (Zod/Joi)Built-in (Ajv JSON Schema)
TypeScriptTypes available, not first-classTypeScript-first design
EcosystemLargest - thousands of middlewareGrowing - plugins, not middleware
Async errorsMust wrap in try/catch or use express-async-errorsNative 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);
});

Explore all agents

Browse the full library of specialized AI agents.