Windsurf rules for Express 5 + TypeScript backends: typed app setup and error handler, async controllers without boilerplate, and Zod 4 validation middleware.
.devin/rules/nodejs-backend-for-windsurf.md. Downloaded files need to be placed in that repository path.---
trigger: glob
globs: "**/*"
---
Apply these rules to Node.js backend code using Express and TypeScript. Check the Express major version; this targets Express 5.
## Express setup
```ts
import express, { type ErrorRequestHandler } from 'express';
import cors from 'cors';
import helmet from 'helmet';
import { userRoutes } from './routes/users';
export const app = express();
app.use(helmet());
app.use(cors({ origin: ['https://app.example.com'], credentials: true }));
app.use(express.json({ limit: '100kb' }));
app.use('/api/users', userRoutes);
const errorHandler: ErrorRequestHandler = (err, _req, res, _next) => {
console.error(err);
res.status(500).json({ error: 'Internal error' });
};
app.use(errorHandler);
```
- Register the error handler after all routes, with four parameters.
- Restrict CORS to known origins.
## Controller pattern
```ts
import type { Request, Response } from 'express';
import { userService } from '../services/user.service';
export const userController = {
async getUser(req: Request<{ id: string }>, res: Response) {
const user = await userService.findById(req.params.id);
if (!user) {
res.status(404).json({ error: 'Not found' });
return;
}
res.json(user);
},
};
```
- Express 5 forwards rejected promises from async handlers to the error handler, so no try/catch-and-`next` wrapper is needed. Plain object methods avoid lost `this` when passing methods as handlers.
## Validation
```ts
import type { RequestHandler } from 'express';
import { z } from 'zod';
export const createUserSchema = z.object({
email: z.email(),
name: z.string().min(1),
password: z.string().min(8),
});
export function validate(schema: z.ZodType): RequestHandler {
return (req, res, next) => {
const result = schema.safeParse(req.body);
if (!result.success) {
res.status(400).json({ errors: z.flattenError(result.error).fieldErrors });
return;
}
req.body = result.data;
next();
};
}
```
- In Zod 4, `result.error.errors` no longer exists; use `result.error.issues` or a helper such as `z.flattenError`. `z.string().email()` still works but is deprecated in favour of `z.email()`.
- `req.query` is read-only in Express 5; validate it into a new variable.
Cursor rule for FastAPI projects using Pydantic v2 schemas, SQLAlchemy 2 async_sessionmaker, lifespan cleanup, explicit commits and 409 conflict handling.
Cursor rule for Express 5 APIs in TypeScript with createApp structure, no try/catch boilerplate, Zod 4 validation into res.locals and a safe central error handler.
Cursor rule for Next.js Route Handlers with Zod 4 safeParse validation, auth and ownership checks, awaited params, bounded pagination and one consistent JSON error shape.
Windsurf
backend
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
After upgrading to Zod 4, a Windsurf-maintained Express API returns { errors: undefined } for invalid bodies because the middleware reads result.error.errors.
Update src/middleware/validate.ts, src/controllers/user.ts and src/app.ts per the Node.js backend rule (Express 5, Zod 4).
validate() returns z.flattenError(...).fieldErrors (or mapped issues), schemas use z.email(), controllers are async without try/catch → next, the error handler is a typed ErrorRequestHandler registered last, and CORS lists explicit origins.
Windsurf's rules documentation (docs.windsurf.com/windsurf/cascade/memories) now redirects to Devin documentation, which describes rules for the Devin Local agent in Devin Desktop. Current rules live in .devin/rules/*.md with a trigger setting, and the legacy Windsurf files (.windsurf/rules/*.md, .windsurf/global_rules.md and .windsurfrules) are still imported by default. AGENTS.md files are read as always-on rules.
.devin/rules/nodejs-backend-for-windsurf.mdStarting point, not a tested configuration for your repository. Applied when files matching globs are involved (trigger: glob). Review the instructions, adapt the file paths and run your project's existing checks.
---
trigger: glob
globs: "**/*"
---
Apply these rules to Node.js backend code using Express and TypeScript. Check the Express major version; this targets Express 5.
## Express setup
```ts
import express, { type ErrorRequestHandler } from 'express';
import cors from 'cors';
import helmet from 'helmet';
import { userRoutes } from './routes/users';
export const app = express();
app.use(helmet());
app.use(cors({ origin: ['https://app.example.com'], credentials: true }));
app.use(express.json({ limit: '100kb' }));
app.use('/api/users', userRoutes);
const errorHandler: ErrorRequestHandler = (err, _req, res, _next) => {
console.error(err);
res.status(500).json({ error: 'Internal error' });
};
app.use(errorHandler);
```
- Register the error handler after all routes, with four parameters.
- Restrict CORS to known origins.
## Controller pattern
```ts
import type { Request, Response } from 'express';
import { userService } from '../services/user.service';
export const userController = {
async getUser(req: Request<{ id: string }>, res: Response) {
const user = await userService.findById(req.params.id);
if (!user) {
res.status(404).json({ error: 'Not found' });
return;
}
res.json(user);
},
};
```
- Express 5 forwards rejected promises from async handlers to the error handler, so no try/catch-and-`next` wrapper is needed. Plain object methods avoid lost `this` when passing methods as handlers.
## Validation
```ts
import type { RequestHandler } from 'express';
import { z } from 'zod';
export const createUserSchema = z.object({
email: z.email(),
name: z.string().min(1),
password: z.string().min(8),
});
export function validate(schema: z.ZodType): RequestHandler {
return (req, res, next) => {
const result = schema.safeParse(req.body);
if (!result.success) {
res.status(400).json({ errors: z.flattenError(result.error).fieldErrors });
return;
}
req.body = result.data;
next();
};
}
```
- In Zod 4, `result.error.errors` no longer exists; use `result.error.issues` or a helper such as `z.flattenError`. `z.string().email()` still works but is deprecated in favour of `z.email()`.
- `req.query` is read-only in Express 5; validate it into a new variable.
Ask for a small change to one matching file. Check the tool's loaded context or instruction view, inspect the diff for the requested convention, and run the relevant build or test. A well-formatted file does not guarantee the model follows every instruction.
The docs recommend keeping rules short and specific. The current docs say AGENTS.md, AGENTS.local.md and AGENT.md are read as always-on rules, including files in subdirectories. Use .devin/rules/ when you need glob, model-decision or manual triggers.
Documentation checked 2026-11-05: Devin docs: Local customizations (redirect target of Windsurf memories docs)Windsurf docs: Cascade memories (redirects)
Cursor rule for FastAPI projects using Pydantic v2 schemas, SQLAlchemy 2 async_sessionmaker, lifespan cleanup, explicit commits and 409 conflict handling.
Cursor rule for Express 5 APIs in TypeScript with createApp structure, no try/catch boilerplate, Zod 4 validation into res.locals and a safe central error handler.
Cursor rule for Next.js Route Handlers with Zod 4 safeParse validation, auth and ownership checks, awaited params, bounded pagination and one consistent JSON error shape.