Skip to content

ever-co/ever-works / .agents/skills/nodejs-express-server

nodejs-express-server

Verified

Build production-ready Express.js servers with middleware, authentication, routing, and database integration. Use when creating REST APIs, managing requests/responses, implementing middleware chains, and handling server logic.

references/restful-routes-with-crud-operations.md

View on GitHub
# RESTful Routes with CRUD Operations

## RESTful Routes with CRUD Operations

```javascript
const userRouter = express.Router();

// GET all users (with pagination)
userRouter.get(
	'/',
	authenticateToken,
	asyncHandler(async (req, res) => {
		const { page = 1, limit = 20 } = req.query;
		const users = await User.findAndCountAll({
			offset: (page - 1) * limit,
			limit: parseInt(limit)
		});

		res.json({
			data: users.rows,
			pagination: { page, limit, total: users.count }
		});
	})
);

// GET single user
userRouter.get(
	'/:id',
	authenticateToken,
	asyncHandler(async (req, res) => {
		const user = await User.findByPk(req.params.id);
		if (!user) return res.status(404).json({ error: 'Not found' });
		res.json({ data: user });
	})
);

// POST create user
userRouter.post(
	'/',
	asyncHandler(async (req, res) => {
		const { email, password } = req.body;
		const hashedPassword = await bcrypt.hash(password, 10);

		const user = await User.create({
			email,
			password: hashedPassword
		});

		res.status(201).json({ data: user });
	})
);

// PATCH update user
userRouter.patch(
	'/:id',
	authenticateToken,
	asyncHandler(async (req, res) => {
		const user = await User.findByPk(req.params.id);
		if (!user) return res.status(404).json({ error: 'Not found' });

		await user.update(req.body, {
			fields: ['email', 'role']
		});

		res.json({ data: user });
	})
);

// DELETE user
userRouter.delete(
	'/:id',
	authenticateToken,
	asyncHandler(async (req, res) => {
		const user = await User.findByPk(req.params.id);
		if (!user) return res.status(404).json({ error: 'Not found' });

		await user.destroy();
		res.status(204).send();
	})
);

app.use('/api/users', userRouter);
```
deprecation-and-migrationaddyosmani/agent-skillsManages deprecation and migration. Use when removing old systems, APIs, or features. Use when migrating users from one implementation to another. Use when migrating a database schema in production, such as renaming or dropping a column without downtime (expand/contract). Use when deciding whether to maintain or sunset existing code.Stars: 119kUpdated Claude CodeCodexVerifiedInstall
performance-optimizationaddyosmani/agent-skillsOptimizes application performance across frontend, backend, queries, and databases. Use when performance requirements exist, when you suspect performance regressions, when Core Web Vitals or load times need improvement, when N+1 query patterns need fixing, or when profiling reveals bottlenecks.Stars: 119kUpdated Claude CodeCodexVerifiedInstall
agent-cost-reportthedotmack/claude-memBelievable agent cost report for any period, default the last 7 full days PT, not counting today. Measured tokens from Claude Code transcripts priced at OpenRouter list prices (ESTIMATED), measured provider spend when a sanctioned source exists, note-taker cost separate, Timing-style HTML/PDF plus report.json, line-items.csv, evidence.json.Stars: 118kUpdated Claude CodeCodexVerifiedInstall
agent-cost-reportthedotmack/claude-memBelievable agent cost report for any period, default the last 7 full days PT, not counting today. Measured tokens from Claude Code transcripts priced at OpenRouter list prices (ESTIMATED), measured provider spend when a sanctioned source exists, note-taker cost separate, Timing-style HTML/PDF plus report.json, line-items.csv, evidence.json.Stars: 118kUpdated Claude CodeCodexVerifiedInstall