Reputation: 1397
I want to change an entity name from Person to Individual. I want to keep the old /person
endpoint (for temporary backward compatibility) and add a new /individual
endpoint.
What would be the easiest way to do it in Node.js using Nest?
I can copy the code but I'm hoping for a better solution that won't require duplication
Upvotes: 9
Views: 14500
Reputation: 11681
In NestJS, We can have multiple routes for the whole controller or for a single route. This is supported for all HTTP methods (POST, GET, PATCH etc., )
@Controller(['route-1', 'route-2'])
export class IndividualController {
@Get(['/sub-route-1','/sub-route-2'])
public async getSomething(...){...}
All HTTP methods support either a single string route or an array of string routes. We could use this technique to deprecate a bad route and start introducing a better route without breaking the consumers immediately.
Upvotes: 11
Reputation: 6665
The @Controller()
decorator accepts an array of prefix, thus you can use like this:
import { Controller, Get } from '@nestjs/common';
@Controller(['person', 'individual'])
export class IndividualController {
@Get()
findAll(): { /* ... */ }
}
to me this is the easiest way.
Upvotes: 18
Reputation: 241
if you mean expressjs
instead of jestjs
(which is a testing framework), my approach would be the following:
simply exclude your controller code into a function and pass it to your routes.
// your controller code
const doSomethingWithPersonEntity = (req, res, next) => {
res.status(200).json(persons);
}
router.get("/person", doSomethingWithPersonEntity);
router.get("/individual", doSomethingWithPersonEntity);
Upvotes: 0