Node.js v produkci vyžaduje jiný přístup než localhost. Error handling, graceful shutdown, clustering.
Error handling¶
// Global uncaught handler process.on(‘uncaughtException’, (err) => { logger.fatal(‘Uncaught exception’, err); process.exit(1); }); // Async error handling app.use(async (err, req, res, next) => { logger.error(‘Request error’, { err, path: req.path }); res.status(err.status || 500).json({ error: ‘Internal error’ }); });
Graceful shutdown¶
const server = app.listen(3000); process.on(‘SIGTERM’, async () => { logger.info(‘SIGTERM received, shutting down…’); server.close(() => { db.close(); process.exit(0); }); setTimeout(() => process.exit(1), 10000); });
Security¶
const helmet = require(‘helmet’); app.use(helmet()); // Security headers // Rate limiting, input validation (zod), prepared statements // Viz security články v Know-How sekci
Klíčový takeaway¶
Graceful shutdown, global error handling, helmet, structured logging. Node.js v produkci ≠ node app.js.