How do I configure CORS middleware when using the forestadmin agent?

How do I configured CORS middleware when using the forestadmin agent?

I need to return the access-control-allow-origin header.

Here’s is my index.js, what should I add?

const agent = createAgent({
  // Security tokens
  authSecret: process.env.FOREST_AUTH_SECRET,
  envSecret: process.env.FOREST_ENV_SECRET,

  // Make sure to set NODE_ENV to 'production' when you deploy your project
  isProduction: process.env.NODE_ENV === 'production',

  // Autocompletion of collection names and fields
  typingsPath: './typings.ts',
  typingsMaxDepth: 5,
});


// Connect your datasources
// All options are documented at https://docs.forestadmin.com/developer-guide-agents-nodejs/data-sources/connection
agent.addDataSource(
  createSqlDataSource({
    uri: process.env.DATABASE_URL,
    schema: process.env.DATABASE_SCHEMA,
    sslMode: process.env.DATABASE_SSL_MODE,
  }),
);

// Expose an HTTP endpoint.
agent.mountOnStandaloneServer(Number(process.env.APPLICATION_PORT));

// Start the agent.
agent.start().catch(error => {
  console.error('\x1b[31merror:\x1b[0m Forest Admin agent failed to start\n');
  console.error('');
  console.error(error.stack);
  process.exit(1);
});

Hi @ZavenArra and welcome to our community ! :slight_smile:

Could you give me more information about what you need to handled with the CORS ?
Do you encounter any issue that could be clarify here ?

Best regards,

Shohan RAHMAN

I need to add CORS settings such as Access-Control-Allow-Origin to be served by the agent.

I am running forestadmin backend API behind an AWS API Gateway and Load Balancer using ECS. I need to return certain CORS headers that the API Gateway will not return according to AWS documention.

Is there documentation about how to add whatever custom CORS settings I require to the forestadmin agent? The documentation is very sparse about this, and there does not seem to be any dedicated support for this product other than this message board, even though I am paying customer.

I’m having a disappointing experience deploying to staging and production due to this problem, which our team has been stuck on for 5 days now.

OK I read up a bunch and solved this issue.

agent.mountOnExpress(app);

app.use(function (req, res, next) {
res.header(“Access-Control-Allow-Origin”, “https://app.forestadmin.com”);
next();
});

I am glad that you found the solution.
I will mark this thread as resolve.

Best regards,

Shohan