返回提示词目录第 1294 页

迁移应用至第五版框架并验证兼容性

梳理应用升级中的路由与接口变更,修复兼容问题并产出测试结果及上线观察方案。

完整提示词

<context>
Express 5 is small in features but has breaking changes that fail quietly. Route paths now use a newer path-to-regexp: wildcards must be named (`*` becomes `/*splat`, and `/{*splat}` if the root should match too), optional parts use braces (`/:file{.:ext}` instead of `:ext?`), and regular-expression characters inside string paths are no longer allowed. Removed or renamed APIs: `app.del` (use `app.delete`), `req.param()`, `res.sendfile` (use `res.sendFile`), `res.send(status)` with a number, `res.json(obj, status)` and similar two-argument forms, `res.redirect('back')`, and the singular `req.acceptsCharset`, `req.acceptsEncoding` and `req.acceptsLanguage` (use the plural forms). `req.query` is a getter and cannot be reassigned, which breaks sanitising middleware that overwrites it; the default query parser is now "simple", so `a[b]=1` no longer becomes a nested object. `req.body` is `undefined` when no body parser ran, `express.urlencoded` defaults to `extended: false`, `res.status()` rejects invalid codes, and `express.static` ignores dotfiles by default. A rejected promise in a handler or middleware now goes to the error handler, which makes wrappers like express-async-errors unnecessary but also changes what reaches error middleware. Express 5 needs Node 18 or newer. The Express team publishes a migration guide and codemods; codemods cover renames, not path syntax intent.
</context>

<task>
Upgrade this app from Express 4 to Express 5.
{{#notes}}
Notes:
{{notes}}
{{/notes}}

1. Read the app setup, every router and route definition, all middleware (own and third-party), error handlers and the HTTP tests. Confirm the Node version meets Express 5's minimum.
2. Inventory: route strings with `*`, `?`, `+`, `(` or regex characters; removed methods; code that assigns to `req.query` or relies on nested query objects; code that assumes `req.body` is an object; `res.redirect('back')`; async error wrappers.
3. Check each third-party middleware for Express 5 support and list incompatible ones with a replacement or decision.
4. Upgrade Express (and `@types/express` if used). Apply the official codemods if available, then fix route paths by hand, matching the old intent for each, and add a test for each changed path including edge cases such as the root path and trailing segments.
5. Fix the rest: renamed response methods, query handling (set the query parser explicitly if nested objects are needed), body guards, and remove async wrappers once you confirm rejections reach the error handler.
6. Run the full test suite and hit every changed route manually or with requests, comparing status codes and bodies with the Express 4 version.
</task>

<constraints>
- Do not change route matching behaviour unintentionally; list any route whose matching changed and why.
- Do not restore the extended query parser without noting its prototype-pollution and nesting implications for validation.
- Do not remove error-handling wrappers before tests prove rejections are handled.
- Only cite breaking changes confirmed in the Express 5 migration guide or seen failing.

{{> guardrails/verify-before-done}}
{{> guardrails/scope-discipline}}
</constraints>

<output_format>
## Inventory
Table: change category, locations, fix.
## Changes made
Dependency changes, route path rewrites (old and new side by side), code changes.
## Behaviour changes
Anything that now behaves differently for clients, or "None".
## Verification
Tests added and run, manual route checks, middleware compatibility results.
## Rollout
Deploy plan and what to watch (404 rates, 500s, error handler logs).
</output_format>

可替换内容

  • {{#notes}}
  • {{notes}}
  • {{/notes}}
  • {{> guardrails/verify-before-done}}
  • {{> guardrails/scope-discipline}}
  • [b]

分类与标签

编程开发、框架升级、路由兼容、回归测试、上线验证

前往 AI 图片创作