Node.js Express API Skill
REST API Patterns
Route Structure
Organize routes by resource (e.g., /api/stocks, /api/market, /api/health).
// routes/stocks.js
const express = require('express');
const router = express.Router();
router.get('/', async (req, res, next) => {
try {
const data = await stockService.getAll();
res.json({ success: true, data });
} catch (error) {
next(error);
}
});
module.exports = router;
Error Handling Middleware
Always use centralized error handling:
// middleware/errorHandler.js
const errorHandler = (err, req, res, next) => {
console.error(err.stack);
res.status(err.status || 500).json({
success: false,
error: err.message || 'Internal Server Error'
});
};
Middleware Patterns
Request Validation
const validateRequest = (schema) => (req, res, next) => {
const { error } = schema.validate(req.body);
if (error) return res.status(400).json({ error: error.message });
next();
};
Rate Limiting
const rateLimit = require('express-rate-limit');
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100
});
app.use('/api/', limiter);
Background Job Scheduling
Using node-cron
const cron = require('node-cron');
// Run every 5 minutes during market hours (10 AM - 3 PM Nepal time)
cron.schedule('*/5 10-15 * * 0-4', async () => {
await updateStockData();
}, { timezone: 'Asia/Kathmandu' });
Using node-schedule
const schedule = require('node-schedule');
// Run at specific times
const job = schedule.scheduleJob('0 10 * * 0-4', async () => {
await marketOpenTasks();
});
Best Practices
- Use async/await - Always wrap async handlers in try-catch
- Validate input - Never trust user input
- Log everything - Use Winston or similar for structured logging
- Use environment variables - Never hardcode secrets
- Implement health checks -
/api/healthendpoint for monitoring
Source: Rojan248/nepse-stock-website — distributed by TomeVault.