Skip to content

Did you know?

In Express 5, app.options('*', cors()) crashes with a PathError; use '/{*splat}' or just app.use(cors()).

Replace app.options('*') with app.options('/{*splat}') in Express 5, or rely on app.use(cors()).

Explained

Countless CORS tutorials add app.options('*', cors()) to answer preflight requests. In Express 5, which uses a newer path-to-regexp, a bare * is not a valid route any more and the app crashes on startup:

PathError: Missing parameter name at index 1: *; visit https://git.new/pathToRegexpError for info

Usually you can delete the line: app.use(cors()) already answers OPTIONS for every route. If you need a catch-all route, name the wildcard:

app.options('/{*splat}', cors());

We tested this on Express 5.3.0 (crash) and 4.22.3 (still accepts *). The same rule applies to any Express 5 route that used a bare *.

Learn more

← All tech tips · Suggest a fix

Back to top