Overriding route not working

Hi there

Expected behavior

Override a single Forest route as it is mentionned here

Actual behavior

The override doesn’t do any effect


Trying to override my Transaction list route using this function in my controller :

  findAll(@Req() req: Request & { user: object }, @Res() res: Response) {
    const { query, user } = req;
    query.searchExtended = '1';

    const recordsGetter = new RecordsGetter(TransactionModel, user, query);
      .then(records => recordsGetter.serialize(records))
      .then(recordsSerialized => res.send(recordsSerialized));

But I don’t see any effect… Do you have any idea ? We are using NestJs in our project
Thanks :pray:

  • Package Version: 8.4.7
  • NestJs Version: 7.5.5
  • Sequelize Version: 6.7.0
  • Database Dialect: postgresql
  • Database Version:
  • Project Name: Swan Back-Office

Hi @Quentin_Giraud,

The route override feature described in the docs relates the modification of generated code.

Override route code looks the following:

const router = express.Router();
const permissionMiddlewareCreator = new PermissionMiddlewareCreator('companies');

router.get('/companies', permissionMiddlewareCreator.list(), (request, response, next) => {
  //custom code here

We are not handling nestjs, but as far I understand, it’s an overlay of express.
I would suggest the following:

  • Is the normal route working alone?
  • Is the normal route still working when you add the nestjs route override?
  • Does a console.log added at the beginning of the findAll method show something when the route is invoked?
  • can you set the environment variable DEBUG=express* and verify if/how the route is added?
  • Maybe the forest prefix is missing.
  • Maybe the case is incorrect
  • Are you sure the nestjs route declaration is made at the proper place from an express point of view?


Additional comment: did you notice the usage of permissionMiddlewareCreator? This is the security guards and should be kept in every circonstances.

1 Like

Thanks for your help @Sliman_Medini

Yes, NestJs is just an overlay of Express

Concerning the usage of permissionMiddlewareCreator , I added a middleware to handle that manually :

export class TransactionPermissionMiddleware implements NestMiddleware {
  async use(req, res, next) {
    const permissionMiddleware = new forest.PermissionMiddlewareCreator('Transaction');
    return permissionMiddleware.list()(req, res, next);

And then :

consumer.apply(ensureAuthenticated, TransactionPermissionMiddleware).forRoutes({ path: "forest/Transaction", method: RequestMethod.GET });

I made different tests :

  • The route is working anyway I have the override or not
  • adding a console.log : didn’t fire
  • I made a modification in resources.js file in order to make the list function calling next() function instead of returning the response.send(records). In this case my console.log is fired. I don’t know how can I change the order…

Hi @Quentin_Giraud,

Ok, good news it is evolving :slight_smile:
So if it’s an overlay it’s certainly possible to:

  • in a first step, make it work with express only as in the examples doc
  • in a second step, try to introduce the NestJs format
    Express is calling middlewares in the order they are added.
    It seems mandatory to look at the NestJs documentation to know which NestJs snippet produces which express equivalent