Skip to main content

nodejs-patterns

Async control flow, error handling, module design, streams, and production hardening

Informações da origem

Repositório
cosmicstack-labs/mercury-agent-skills
Última atividade na origem
10 de maio de 2026 às 06:29
Idioma detectado do SKILL.md
inglês
Estrelas
476
Forks
63

Opções de instalação

Por padrão, está selecionado o prompt que primeiro revisa a origem. Você pode mudar para um comando direto ou baixar uma cópia local.

Revise os arquivos de origem

Leia o SKILL.md e os arquivos complementares exibidos pelo SkillsMP antes de decidir se vai instalar.

Exibindo SKILL.md

SKILL.md
Instruções da origem · Visualização somente leitura
name
nodejs-patterns
description
Async control flow, error handling, module design, streams, and production hardening
metadata
{"author":"cosmicstack-labs","version":"1.0.0","category":"backend","tags":["nodejs","backend","javascript","async","server"]}
# Node.js Patterns Production-grade Node.js backend patterns. ## Async Patterns ### Prefer Async/Await ```javascript // Good async function getUser(id) { const user = await db.findUser(id); return user; } // Avoid raw callbacks or.then() chains ``` ### Parallel Execution ```javascript // Sequential (slow) const user = await fetchUser(id); const posts = await fetchPosts(id); // Parallel (fast) const [user, posts] = await Promise.all([ fetchUser(id), fetchPosts(id), ]); ``` ## Error Handling ### Global Handler ```javascript process.on('uncaughtException', (err) => { console.error('Uncaught:', err); // Graceful shutdown server.close(() => process.exit(1)); }); process.on('unhandledRejection', (reason) => { console.error('Unhandled Rejection:', reason); }); ``` ### Express Error Middleware ```javascript app.use((err, req, res, next) => { const status = err.status || 500; res.status(status).json({ error: err.message, ...(process.env.NODE_ENV === 'development' && { stack: err.stack }), }); }); ``` ## Module Design - Single responsibility per module - Export a class or factory function, not plain objects - Use dependency injection for testability - Prefer CommonJS (require) or ESM (import), consistent throughout ## Production Hardening - Cluster mode for multi-core - Process manager (PM2) with auto-restart - Health check endpoints - Graceful shutdown (SIGTERM handler) - Memory limit monitoring - Structured logging (pino/winston)
Ver no GitHub