express
When to use this skill
Use this skill whenever the user wants to:
- Build Node.js HTTP servers with Express routing and middleware
- Configure CORS, body parsing, error handling, and static files
- Create REST APIs with request validation and response formatting
- Set up production-ready Express applications with security headers
How to use this skill
Workflow
- Create app — instantiate Express and configure middleware
- Define routes — set up route handlers for each endpoint
- Add error handling — implement error middleware for consistent responses
- Deploy — run behind a reverse proxy with HTTPS
Quick Start Example
const express = require('express');
const cors = require('cors');
const helmet = require('helmet');
const app = express();
// Middleware
app.use(helmet());
app.use(cors());
app.use(express.json());
// Routes
app.get('/api/items', async (req, res, next) => {
try {
const items = await Item.findAll();
res.json({ items });
} catch (err) {
next(err);
}
});
app.post('/api/items', async (req, res, next) => {
try {
const { name, price } = req.body;
const item = await Item.create({ name, price });
res.status(201).json(item);
} catch (err) {
next(err);
}
});
// 404 handler
app.use((req, res) => {
res.status(404).json({ error: 'Not found' });
});
// Error middleware (must have 4 params)
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(err.status || 500).json({
error: err.message || 'Internal server error',
});
});
app.listen(3000, () => console.log('Server running on port 3000'));
Async Error Wrapper
// Wrap async handlers to catch rejected promises
const asyncHandler = (fn) => (req, res, next) =>
Promise.resolve(fn(req, res, next)).catch(next);
app.get('/api/users', asyncHandler(async (req, res) => {
const users = await User.findAll();
res.json(users);
}));
Router Example
// routes/items.js
const router = require('express').Router();
router.get('/', asyncHandler(async (req, res) => { /* ... */ }));
router.post('/', asyncHandler(async (req, res) => { /* ... */ }));
router.get('/:id', asyncHandler(async (req, res) => { /* ... */ }));
module.exports = router;
// app.js
app.use('/api/items', require('./routes/items'));
Best Practices
- Separate routes and middleware into modules; use
express.Router()for organization - Always wrap async handlers with try/catch or a wrapper to avoid unhandled rejections
- Use
helmetfor security headers and configure CORS for production origins - Deploy behind a reverse proxy (nginx) with HTTPS in production
- Use
morganfor request logging and structured error responses
Reference
- Official documentation: https://expressjs.com/
Keywords
express, Node.js, middleware, routing, REST API, error handling, async, helmet, cors
More from partme-ai/full-stack-skills
vite
Guidance for Vite using the official Guide, Config Reference, and Plugins pages. Use when the user needs Vite setup, configuration, or plugin selection details.
68element-plus-vue3
Provides comprehensive guidance for Element Plus Vue 3 component library including installation, components, themes, internationalization, and API reference. Use when the user asks about Element Plus for Vue 3, needs to build Vue 3 applications with Element Plus, or customize component styles.
63vue3
Guidance for Vue 3 using the official guide and API reference. Use when the user needs Vue 3 concepts, patterns, or API details to build components, apps, and tooling.
54electron
Build cross-platform desktop applications with Electron, covering main/renderer process architecture, IPC communication, BrowserWindow management, menus, tray icons, packaging, and security best practices. Use when the user asks about Electron, needs to create desktop applications, implement Electron features, or build cross-platform desktop apps.
52uniapp-project
Provides per-component and per-API examples with cross-platform compatibility details for uni-app, covering built-in components, uni-ui components, and APIs (network, storage, device, UI, navigation, media). Use when the user needs official uni-app components or APIs, wants per-component examples with doc links, or needs platform compatibility checks.
40ascii-cli-logo-banner
Entry point for ASCII CLI banners that routes to the Python built-in font skill or figlet.js/FIGfont skill. Use when the user wants a startup banner, ASCII logo, terminal welcome screen, or CLI branding for a service.
38