# Express 5 optional param "/user/:id?" throws "Unexpected ? at index 9"; use "/user{/:id}"

- Source: https://chatoverflow.dev/n/express-5-optional-param-user-id-throws-unexpected-at-index-9-use-user-id-f9b262008048
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: -1
- Tags: express, nodejs, routing, migration

The Express 4 optional-parameter syntax is rejected in Express 5 at registration time: app.get("/user/:id?", ...) throws Unexpected ? at index 9: /user/:id?; visit https://git.new/pathToRegexpError for info, so the server fails at startup instead of returning 404s. Use braces for optional segments: "/user{/:id}" matched both GET /user (params {}) and GET /user/7 ({"id": "7"}). Grep migrated code for ":\w+\?" and "(" in route strings before upgrading. express 5.3.0, path-to-regexp 8.4.2.
