Problems integrating into existing server running Express/Sequelize

I already refoactored it after you’ve sent over the code and asked if it was present on the app.js, so this definitely isn’t the issue.

Can you maybe share the new version of your app.js? Because the error received on https://server2.timespex.com/forest seems to be coming from this code new Error('Not allowed by CORS')

Sure, here you go :

require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const useragent = require('express-useragent');
const http = require('http');
const forest = require('forest-express-sequelize');
const jwt = require('express-jwt');

const app = express();

const db = require('./models');
db.sequelize.sync();

let allowedOrigins = [/\.forestadmin\.com$/, /localhost:\d{4}$/];

if (process.env.CORS_ORIGINS) {
  allowedOrigins = allowedOrigins.concat(process.env.CORS_ORIGINS.split(','));
}

const corsConfig = {
  origin: allowedOrigins,
  allowedHeaders: ['Authorization', 'X-Requested-With', 'Content-Type'],
  maxAge: 86400, // NOTICE: 1 day
  credentials: true,
};
app.use('/forest/authentication', cors({
  ...corsConfig,
  // The null origin is sent by browsers for redirected AJAX calls
  // we need to support this in authentication routes because OIDC
  // redirects to the callback route
  origin: corsConfig.origin.concat('null'),
}));
app.use(cors(corsConfig));

const server = http.createServer(app);


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

server.listen(process.env.PORT || '3001', () => {
  console.log(`Server is running on port: ${process.env.PORT || '3001'}`);
  app.use(async () => await forest.init({
    envSecret: process.env.FOREST_ENV_SECRET,
    authSecret: process.env.FOREST_AUTH_SECRET,
    objectMapping: db.Sequelize,
    connections: { default: db.sequelize },
  }));
});


app.use(jwt({
  secret: process.env.FOREST_AUTH_SECRET,
  algorithms: ['RS256'],
  credentialsRequired: false,
}));


module.exports = app;



When I try to reach the https://server2.timespex.com/forest I get a 504: Gateway timeout error from Nginx, soit looks like a misconfiguration on your server

@olesyak
You are getting a 504 because the route is not handled in any way on the server. If I stub the route to return a 204 status code, the request works fine. So my question remains the same from earlier, should i do something with the route /forest or not and if not then how is it supposed to be handled.

After checking your code, I can see that you pass an async function to app.use

Could you replace this part with

app.use(await forest.init({,
  envSecret: process.env.FOREST_ENV_SECRET,
  authSecret: process.env.FOREST_AUTH_SECRET,
  objectMapping: models,
  connections: { default: connection },
}))

The routes are defined and added by this code snipped, so passing an async function could explain the absence of routes in your project.

Let me know if it helps

I tried this in the first place, as it was specified in your documentation, but this syntax is invalid:
image

Could you please try using this code snippet? I declared the server.listen callback function as async

Thank you

It finally works, thank you so much! :smile: You should fix the documentation though, although it was kind of obvious :smile: :ok_hand: