nodejs-expert
Expert-level Node.js backend development with Express, async patterns, streams, performance optimization, and production best practices
What this skill does
# Node.js Expert
You are an expert in Node.js with deep knowledge of async programming, streams, event loop, Express framework, and production deployment. You build scalable, performant backend applications following Node.js best practices.
## Core Expertise
### Modern Node.js Features
**ESM (ES Modules):**
```javascript
// package.json
{
"type": "module" // Enable ESM
}
// Import with ESM
import express from 'express';
import { readFile } from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
// __dirname equivalent in ESM
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Dynamic imports
if (condition) {
const module = await import('./optional-module.js');
module.doSomething();
}
// Top-level await (ESM only)
const data = await readFile('config.json', 'utf-8');
const config = JSON.parse(data);
```
**Async/Await Patterns:**
```javascript
// Parallel execution
async function fetchAllData() {
const [users, posts, comments] = await Promise.all([
fetchUsers(),
fetchPosts(),
fetchComments()
]);
return { users, posts, comments };
}
// Sequential execution
async function processItems(items) {
const results = [];
for (const item of items) {
const result = await processItem(item);
results.push(result);
}
return results;
}
// Error handling
async function safeOperation() {
try {
const result = await riskyOperation();
return { success: true, data: result };
} catch (error) {
console.error('Operation failed:', error);
return { success: false, error: error.message };
}
}
// Promise.allSettled for mixed results
async function fetchMultiple(urls) {
const results = await Promise.allSettled(
urls.map(url => fetch(url).then(r => r.json()))
);
return results.map(result => {
if (result.status === 'fulfilled') {
return { success: true, data: result.value };
} else {
return { success: false, error: result.reason.message };
}
});
}
```
### Express Framework
**Basic Server:**
```javascript
import express from 'express';
import helmet from 'helmet';
import cors from 'cors';
import compression from 'compression';
import morgan from 'morgan';
const app = express();
// Middleware
app.use(helmet()); // Security headers
app.use(cors()); // CORS
app.use(compression()); // Response compression
app.use(morgan('combined')); // Logging
app.use(express.json()); // Parse JSON bodies
app.use(express.urlencoded({ extended: true }));
// Routes
app.get('/api/users', async (req, res, next) => {
try {
const users = await getUsersFromDB();
res.json(users);
} catch (error) {
next(error);
}
});
app.get('/api/users/:id', async (req, res, next) => {
try {
const { id } = req.params;
const user = await getUserById(id);
if (!user) {
return res.status(404).json({ error: 'User not found' });
}
res.json(user);
} catch (error) {
next(error);
}
});
app.post('/api/users', async (req, res, next) => {
try {
const userData = req.body;
// Validation
if (!userData.email || !userData.name) {
return res.status(400).json({ error: 'Missing required fields' });
}
const user = await createUser(userData);
res.status(201).json(user);
} catch (error) {
next(error);
}
});
// Error handling middleware
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(err.status || 500).json({
error: err.message || 'Internal Server Error'
});
});
// 404 handler
app.use((req, res) => {
res.status(404).json({ error: 'Not Found' });
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
```
**Router Organization:**
```javascript
// routes/users.js
import express from 'express';
import * as userController from '../controllers/users.js';
import { authenticate } from '../middleware/auth.js';
import { validate } from '../middleware/validation.js';
import { userSchema } from '../schemas/user.js';
const router = express.Router();
router.get('/', userController.getUsers);
router.get('/:id', userController.getUser);
router.post(
'/',
authenticate,
validate(userSchema),
userController.createUser
);
router.put('/:id', authenticate, userController.updateUser);
router.delete('/:id', authenticate, userController.deleteUser);
export default router;
// app.js
import userRoutes from './routes/users.js';
app.use('/api/users', userRoutes);
```
**Middleware:**
```javascript
// Authentication middleware
export function authenticate(req, res, next) {
const token = req.headers.authorization?.split(' ')[1];
if (!token) {
return res.status(401).json({ error: 'No token provided' });
}
try {
const decoded = jwt.verify(token, process.env.JWT_SECRET);
req.user = decoded;
next();
} catch (error) {
res.status(401).json({ error: 'Invalid token' });
}
}
// Validation middleware
export function validate(schema) {
return (req, res, next) => {
const { error } = schema.validate(req.body);
if (error) {
return res.status(400).json({
error: error.details[0].message
});
}
next();
};
}
// Rate limiting
import rateLimit from 'express-rate-limit';
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // Limit each IP to 100 requests per windowMs
message: 'Too many requests from this IP'
});
app.use('/api/', limiter);
// Request logging
export function requestLogger(req, res, next) {
const start = Date.now();
res.on('finish', () => {
const duration = Date.now() - start;
console.log(`${req.method} ${req.path} ${res.statusCode} - ${duration}ms`);
});
next();
}
```
### File System Operations
**File Operations:**
```javascript
import { readFile, writeFile, appendFile, unlink, mkdir } from 'node:fs/promises';
import { existsSync } from 'node:fs';
import path from 'node:path';
// Read file
async function readConfig() {
try {
const data = await readFile('config.json', 'utf-8');
return JSON.parse(data);
} catch (error) {
if (error.code === 'ENOENT') {
console.error('File not found');
}
throw error;
}
}
// Write file
async function saveConfig(config) {
const data = JSON.stringify(config, null, 2);
await writeFile('config.json', data, 'utf-8');
}
// Append to file
async function logMessage(message) {
const timestamp = new Date().toISOString();
await appendFile('app.log', `${timestamp} - ${message}\n`);
}
// Create directory (recursive)
async function ensureDir(dirPath) {
if (!existsSync(dirPath)) {
await mkdir(dirPath, { recursive: true });
}
}
// Delete file
async function deleteFile(filePath) {
try {
await unlink(filePath);
} catch (error) {
if (error.code !== 'ENOENT') {
throw error;
}
}
}
// Read directory
import { readdir } from 'node:fs/promises';
async function listFiles(dirPath) {
const files = await readdir(dirPath, { withFileTypes: true });
return files
.filter(file => file.isFile())
.map(file => file.name);
}
```
**Streams:**
```javascript
import { createReadStream, createWriteStream } from 'node:fs';
import { createGzip } from 'node:zlib';
import { pipeline } from 'node:stream/promises';
// Read large file with stream
function processLargeFile(filePath) {
const stream = createReadStream(filePath, { encoding: 'utf-8' });
stream.on('data', (chunk) => {
console.log('Received chunk:', chunk.length);
});
stream.on('end', () => {
console.log('Finished reading file');
});
stream.on('error', (error) => {
console.error('Error reading file:', error);
});
}
// Transform stream
import { Transform } from 'node:stream';
class UpperCaseTransform extends Transform {
_transform(chunk, encoding, callback) {
this.push(chunk.toString().toUpperCase());
callback();
}
}
// Pipeline (recommended way)
async functionRelated in languages
csharp-expert
IncludedExpert-level C# development with .NET 8+, ASP.NET Core, LINQ, async/await, and enterprise patterns
java-expert
IncludedExpert-level Java development with Java 21+ features, Spring Boot, Maven/Gradle, and enterprise best practices
pcl-expert
IncludedExpert in Persona Control Language (PCL) - language design, compiler architecture, runtime systems, and ecosystem development
php-expert
IncludedExpert-level PHP development with PHP 8+, Laravel, Composer, and modern best practices
rust-expert
IncludedExpert-level Rust development with ownership, lifetimes, async, error handling, and production-grade patterns
go-expert
IncludedExpert-level Go development with Go 1.22+ features, concurrency, standard library, and production-grade best practices