Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Forest Admin properly with nodeJs server? (with forest-express-mongoose)

I'm trying to run a server programmed in NodeJS locally on my laptop and interact with Forest Admin using the forest-express-mongoose package. The DB is a local MongoDB.

So I run my server and it is able to connect with an app I made perfectly. It connects to the mongoDB database properly as well. So then I want to use Forest Admin. I'm not so familiarized with Forest but I think I don't have to install Forest admin as an npm project cause that would create another separated project and in this case I'm using forest-express-mongoose from inside my server. Now what I don't know is how to get the Forest admin panel?

My server runs but it gives me an error because the env key is not associated with any forest panel: (Forest server request error: Not Found)

That is indeed to be expected since I have to create a project from inside the Forest website. But then when I want to create one, the installation process wants me to create and install another separated server. That is not what I want, I want to integrate it with my existing server. How can I achieve this?

Here's my server's code:

require('dotenv').config()

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

// create express app
const app = express();

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }))

// parse application/json
app.use(bodyParser.json())

// Configuring the database
const dbConfig = require('./config/database.config.js');
const mongoose = require('mongoose');

const cors = require('cors');

mongoose.Promise = global.Promise;

// Connecting to the database
mongoose.connect(process.env.MONGODB_URI || dbConfig.url, {
    useNewUrlParser: true
}).then(() => {
    console.log("Successfully connected to the database");
}).catch(err => {
    console.log('Could not connect to the database. Exiting now...', err);
    process.exit();
});

// define a simple route
app.get('/', (req, res) => {
    res.json({"message": "Welcome to EasyDonations application. Take donations quickly. Organize and keep track of all your donations."});
});

// Then use it before your routes are set up:
app.use(cors({credentials: true, origin: true}));

require('./app/models/timeframe.js');
require('./app/routes/institution.routes.js')(app);
require('./app/routes/offer.routes.js')(app);
require('./app/routes/request.routes.js')(app);
require('./app/routes/user.routes.js')(app);
require('./app/routes/volunteer.routes.js')(app);
require('./app/routes/donation.routes.js')(app);

// listen for requests
app.listen(process.env.PORT || 3000, () => {
    console.log("Server is listening on port 3000");
});

app.use(require('forest-express-mongoose').init({
    modelsDir: __dirname + '/app/models',
    envSecret: 'xxx', // the key I obviously don't have
    authSecret: 'testeando', // I think I can put any string for development purposes, otherwise I don't know were to get authSecret
    mongoose: require('mongoose'),
}));

My package.json has the following dependencies:

"dependencies": {
  "async": "^2.6.2",
  "body-parser": "^1.18.3",
  "cors": "^2.8.5",
  "dotenv": "^6.2.0",
  "express": "^4.16.4",
  "forest-express-mongoose": "^2.16.1",
  "mongodb": "^3.1.13",
  "mongoose": "5.1.4",
  "mongoose-schema-extend": "^0.2.2"
}

I'm aware I'm using some old dependencies but if I start changing them, the project starts having issues.

So I'm sort of in a vicious cycle here. I need the Forest admin created to make my server work but the Forest admin creation process wants me to create a separated server.

Or maybe I also need to run a Forest server? I tried that and turns out if I run forest first it gets data from my DB but then when I run my server it changes its models and it gives me an error "The server encountered an error".

like image 752
Flama Avatar asked Nov 06 '22 04:11

Flama


1 Answers

I managed to run my server after simply running it and on an already created project on forest setting a new envirornment with its server set as http://localhost:3000

like image 119
Flama Avatar answered Nov 12 '22 16:11

Flama