Cannot override routes in v4

Expected behavior

I’d expect that my custom route implementation is called. It is located under /decorators/routes/reward.js

Actual behavior

My route implementation is not called. The console.log I inserted doesn’t print anything when creating a new reward object.

Context

I’d like to do the following thing: https://docs.forestadmin.com/documentation/v/v4/reference-guide/routes
I did create my /decorator/routes/reward.js file whose content is:

const express = require('express');
const router = express.Router();
const Liana = require('forest-express-sequelize');

router.post('/reward', Liana.ensureAuthenticated, (req, res, next) => {
  console.log('CALLLELDDDD');
  next();
});

module.exports = router;

We have a different structure because our app.js looks like this:

const express = require('express');
const requireAll = require('require-all');

const app = express();
const bodyParser = require('body-parser');

app.use(bodyParser.json({ limit: '2mb' }));
app.use(bodyParser.urlencoded({ extended: true, limit: '2mb' }));

requireAll({
  dirname: `${__dirname}/middlewares`,
  recursive: true,
  resolve: Module => new Module(app),
});

module.exports = app;

and our /middlewares/forestadmin/index.js looks like this:

module.exports = function (app) {
  require('lumber-forestadmin').run(app, {
    modelsDir: __dirname + '/../../models',
    envSecret: process.env.FOREST_ENV_SECRET,
    authSecret: process.env.FOREST_AUTH_SECRET,
    sequelize: models.Sequelize,
    connections: [
      models.sequelize.db1,
      models.sequelize.db2
    ],
  });

  // You MUST require these files before the default routes.
  fs.readdirSync(__dirname + '/../../decorators/routes').forEach((file) => {
    if (file[0] !== '.') {
      app.use('/forest', require(`${__dirname}/../../decorators/routes/${file}`));
    }
  });

  requireAll({
    dirname: __dirname + '/../../routes',
    recursive: true,
    resolve: Module => app.use('/forest', Module)
  });

  console.log(chalk.cyan('Your admin panel is available here: https://app.forestadmin.com/projects'));
};
  • lumber-forestadmin: “^1.4.1”,
  • Express Version: “~4.16.3”,
  • Sequelize Version: “~5.15.1”

Hello @boldizsaremma !
I see that you used lumber-forestadmin to generate your project ? We encourage use to use lumber-cli instead :wink:
What version of forest-express-sequelize are you using ?
I need to setup a project on my end to be able to understand where the error is.

Hello @anon94532230,

I see. :smiley:

It is 4.0.2. Thanks for looking into this! :slight_smile:

You used an old version of lumber to generate your project, I won’t be able to create a project, the easiest way to fix this is to re-generate your project using the latest version of lumber.
Then you’ll be easily able to override the routes :slight_smile:

We have a complex project already with smart actions, fields etc. Will anything break once we regenerate the project?

You’ll need to follow the steps in the documentation, upgrading will bring you new features, improved security etc.
You need to follow these steps:

  • Generate your project with the latest version of lumber
  • Copy everything you customized onto the new project and follow those steps (1 & 2) to avoid any breaking changes
  • Update your .env file with the values from your current project.
  • Test it thoroughly on your dev environment before deploying it to production.

With this, you’ll have an up-to-date project, and you can benefit from our latest features.

Hi @boldizsaremma, as a bonus, here are all the changes we’ve made regarding the architecture of the admin backend generated with the latest version of lumber - https://www.notion.so/Lumber-v2-to-v3-0bf626b7a1ab4cedaec9f712b676ce8b

Let us know if you need help with the transition.

Okay, thanks. I’ll try and will message you if there’s something not working. :slight_smile:

3 Likes