How do you implement API versioning in Express.js?

Answer

API versioning allows evolving the API without breaking existing clients. Three common approaches in Express: URL path versioning (most common): app.use('/api/v1', v1Router); app.use('/api/v2', v2Router);. Clients explicitly request the version they support. Header versioning: clients send Accept: application/vnd.myapp.v2+json and middleware routes to the appropriate handler. Query string versioning: /api/users?version=2. URL versioning is the simplest and most transparent. Structure v1 and v2 route directories with shared controllers for common logic. When deprecating a version, return a Deprecation response header warning clients, then eventually remove it after sufficient notice.