# Project creation from existing Express / Sequelize NodeJS app

**URL:** <https://community.forestadmin.com/t/project-creation-from-existing-express-sequelize-nodejs-app/2843>\
**Category:** Help me!\
**Created:** [July 5, 2021, 2:16pm UTC](https://community.forestadmin.com/t/project-creation-from-existing-express-sequelize-nodejs-app/2843 "2021-07-05T14:16:38Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![cooki](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/cooki/32/2657_2.png) [@cooki](https://community.forestadmin.com/u/cooki)\
**Post date:** [July 5, 2021, 2:16pm UTC](https://community.forestadmin.com/t/project-creation-from-existing-express-sequelize-nodejs-app/2843/1 "2021-07-05T14:16:38Z")

</div>

Hello, I’m trying to install forest admin into an existing Express / Sequelize app and some things are unclear:

During the installation process, the wizard tells me to add the following to the top of my app.js file:

```javascript
const forest = require('forest-express-sequelize');
const models = require('./path/to/your/sequelize/models');
const connection = require('./path/to/your/sequelize/connection');

// Insert this middleware in your express listen callback:
app.use(await forest.init({
 envSecret: process.env.FOREST_ENV_SECRET,
 authSecret: process.env.FOREST_AUTH_SECRET,
 objectMapping: models,
 connections: { default: connection },
}));

```

But it is unclear to me what the `models` and `connection` variables should be, could you please provide more information ? Maybe if there is a demo app somewhere on github I could use that as a reference.

I am also getting the following warning when running the app:

`[forest] 🌳🌳🌳 Your configDir ("/Users/camillefeghali/dev/foxxbee/fa-demo-mac-sequelize/forest") does not exist. Please make sure it is set correctly.`

Which lends me to believe that I should create a `/forest` directory in my project, but nowhere in the installation steps is that mentioned.

Thanks for your time.

---

<div class="post-metadata">

**Author:** ![anon79585656](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@anon79585656](https://community.forestadmin.com/u/anon79585656)\
**Post date:** [July 5, 2021, 2:55pm UTC](https://community.forestadmin.com/t/project-creation-from-existing-express-sequelize-nodejs-app/2843/2 "2021-07-05T14:55:19Z")

</div>

Hello @cooki,

With a `forest-express-sequelize` integration:

- the `models` is expected to be the Sequelize module require result.
- the `connection` is expected to be the Sequelize instance, created with your database parameters

An exemple of a `models/index.js` file could be something like:

```javascript
const Sequelize = require("sequelize");
const sequelize = new Sequelize(DB_NAME, DB_USER, DB_PASSWORD, {
  host: DB_HOST,
  port: DB_PORT,
  dialect: DB_DIALECT,
  ...
});

const db = {};

db.models = Sequelize; // Capital "s"
db.connection = sequelize; // Lower case

db.items = require("./items.js")(sequelize, Sequelize);

module.exports = db;

```

The documentation snippet shows two require statements in case some project define things separately.  
With the code above, you can do:

```javascript
const forest = require('forest-express-sequelize');

const { connection, models } = require('./models');

app.listen(PORT, async () => {
  console.log(`Server is running on port ${PORT}.`);

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

```

Let me know if this is enough to solve you issue.

---

<div class="post-metadata">

**Author:** ![cooki](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/cooki/32/2657_2.png) [@cooki](https://community.forestadmin.com/u/cooki)\
**Post date:** [July 5, 2021, 3:00pm UTC](https://community.forestadmin.com/t/project-creation-from-existing-express-sequelize-nodejs-app/2843/3 "2021-07-05T15:00:18Z")

</div>

Hey @anon79585656, thanks for the quick reply.  
That worked, thank you. I am now facing another issue. When trying to access my admin panel, I get the following screen:

 ![Screen Shot 2021-07-05 at 4.57.09 PM](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/8/88a83114e81712636d8e586a99be3950b08a46b8.png)

After checking the network tab in chrome dev tools, I see two requests that failed:

 ![Screen Shot 2021-07-05 at 4.58.39 PM](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/c/cb72f6823b262b95a4f4b41f32677f81226f0382.png)

How can we move forward from here ?

---

<div class="post-metadata">

**Author:** ![anon79585656](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@anon79585656](https://community.forestadmin.com/u/anon79585656)\
**Post date:** [July 5, 2021, 3:06pm UTC](https://community.forestadmin.com/t/project-creation-from-existing-express-sequelize-nodejs-app/2843/4 "2021-07-05T15:06:52Z")

</div>

@cooki,

In the onboarding snippets, there should have been a line like:  
`app.use('^(?!forest/?$).*', cors(corsOptions));`

This will allow our authentication requests to use their own CORS configuration.

Did you add it to your project?

---

<div class="post-metadata">

**Author:** ![cooki](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/cooki/32/2657_2.png) [@cooki](https://community.forestadmin.com/u/cooki)\
**Post date:** [July 5, 2021, 3:13pm UTC](https://community.forestadmin.com/t/project-creation-from-existing-express-sequelize-nodejs-app/2843/5 "2021-07-05T15:13:03Z")

</div>

In the onboarding it states that **if** cors is enabled I should add the snippet.

I tried many options:  
1 - Not adding the `app.use('^(?!forest/?$).*', cors(corsOptions));` because I don’t have cors enabled.

2 - Enabling cors and adding `app.use('^(?!forest/?$).*', cors());` =\> Note that I do not pass a corsOptions variable, as it is not described in the wizard what that var should hold

3 - Adding the following code (taken from a standalone PostgreSQL installation app) installed using lumber:

```javascript
let allowedOrigins = [
  /\.forestadmin\.com$/,
  /localhost:\d{4}$/,
  "^(?!forest/?$).*",
];

const corsConfig = {
  origin: allowedOrigins,
  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));

```

Same error in every scenario.

---

<div class="post-metadata">

**Author:** ![morganperre](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/morganperre/32/1296_2.png) [@morganperre](https://community.forestadmin.com/u/morganperre)\
**Post date:** [July 5, 2021, 3:24pm UTC](https://community.forestadmin.com/t/project-creation-from-existing-express-sequelize-nodejs-app/2843/6 "2021-07-05T15:24:52Z")

</div>

Hello @cooki,

Can you share more information (detailed screenshot) of the network calls that fails ?

Thanks in advance,  
Morgan

---

<div class="post-metadata">

**Author:** ![cooki](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/cooki/32/2657_2.png) [@cooki](https://community.forestadmin.com/u/cooki)\
**Post date:** [July 5, 2021, 3:31pm UTC](https://community.forestadmin.com/t/project-creation-from-existing-express-sequelize-nodejs-app/2843/7 "2021-07-05T15:31:23Z")

</div>

Hey @morganperre sure,

 ![Screen Shot 2021-07-05 at 5.30.23 PM](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/e/e94e82c08a7072245c0b4400274a3e2bdcd68201.png)

 ![Screen Shot 2021-07-05 at 5.31.07 PM](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/8/83b28d827dedb6f757b9c39f8c0b44bf41cfc16e.png)

---

<div class="post-metadata">

**Author:** ![morganperre](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/morganperre/32/1296_2.png) [@morganperre](https://community.forestadmin.com/u/morganperre)\
**Post date:** [July 5, 2021, 3:47pm UTC](https://community.forestadmin.com/t/project-creation-from-existing-express-sequelize-nodejs-app/2843/8 "2021-07-05T15:47:35Z")

</div>

Thanks @cooki.

It seems that the last authentication step is failing. Can you confirm that the **/forest/authentication** is **OK**? And only the **/forest/authentication/callback** fails ?

I have more questions to find the culprit.

- On which **PORT** do you run your application ?
- Did you define the environment variables during the onboarding process (`FOREST_ENV_SECRET`, `FOREST_AUTH_SECRET`, `APPLICATION_URL`) ?

Thanks,  
Morgan

---

<div class="post-metadata">

**Author:** ![cooki](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/cooki/32/2657_2.png) [@cooki](https://community.forestadmin.com/u/cooki)\
**Post date:** [July 5, 2021, 3:52pm UTC](https://community.forestadmin.com/t/project-creation-from-existing-express-sequelize-nodejs-app/2843/9 "2021-07-05T15:52:39Z")

</div>

@morganperre I hadn’t added the APPLICATION\_URL in my .env because I couldn’t see where it was used. My panel loads now that I added it.

Could you explain to me where that env var is used ?

Thanks a lot for your help.

---

<div class="post-metadata">

**Author:** ![morganperre](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/morganperre/32/1296_2.png) [@morganperre](https://community.forestadmin.com/u/morganperre)\
**Post date:** [July 5, 2021, 3:57pm UTC](https://community.forestadmin.com/t/project-creation-from-existing-express-sequelize-nodejs-app/2843/10 "2021-07-05T15:57:25Z")

</div>

The `APPLICATION_URL` is used to create the callback URL. If you specified a specific url for your application in place of the default one (for example for an install on a remote machine), this url should be set to the right URL to reach your app.

If think we miss some documentation during the onboarding. I’ll talk to my teammate to review this step. Thanks for the head up.

Have a nice day.  
Morgan
