Node.js in der Produktion erfordert einen anderen Ansatz als auf localhost. Fehlerbehandlung, Graceful Shutdown, Clustering.
Fehlerbehandlung¶
// Globaler Uncaught-Handler process.on(‘uncaughtException’, (err) => { logger.fatal(‘Uncaught exception’, err); process.exit(1); }); // Async-Fehlerbehandlung 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); });
Sicherheit¶
const helmet = require(‘helmet’); app.use(helmet()); // Security Headers // Rate Limiting, Input-Validierung (zod), Prepared Statements // Siehe Security-Artikel in der Know-How-Sektion
Wichtigste Erkenntnis¶
Graceful Shutdown, globale Fehlerbehandlung, Helmet, strukturiertes Logging. Node.js in Produktion ≠ node app.js.