2

I want to completely override a controller route. E. g:

@Controller('shipments')
export class ShipmentsController {

  @Post('/create')
  async find(): Promise<Activities> {
    return service.find()
  }
}

In order to make a request to the previous example, The URL will be: http://localhost:8080/shipments/create

I want to change that URL without moving the controller to another class. For example, I want the URL for that specific function to be http://localhost:8080/whatever/i/want.

Is this possible?

1
  • If the function doesn't correspond with the route, then the function is in a wrong controller. Commented Feb 22, 2021 at 10:32

2 Answers 2

5

This is not possible, and goes against the ideas of the framework of having easy to configure routes with structure and uniformity. If you want a route like that, you can use express on it;s own, or technically add the route in the bootstrap file like so

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.getHttpServer().get('/whatever/you/want', (req, res, next) => {});
  await app.listen(3000);
}

But now you don't have (easy) access to services, testing this is a pain, and generally it's confusing, not to mention no use of any Nest enhancers like interceptors or pipes.

Sign up to request clarification or add additional context in comments.

Comments

0

As others have said, this is not recommended. But one approach is to leave the controller pathless and set on each route

@Controller('user')
...
@Get('me')
@Get(':id')

You can do

@Controller()
...
@Get('profile')
@Get('user/:id')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.