I am trying to connect a MongoDB URI to the strapi backend.
I am able to connect with the main URL but when I created a .env.development variable, I am not able to connect to the database.
{
"defaultConnection": "default",
"connections": {
"default": {
"connector": "mongoose",
"settings": {
"uri": "${process.env.DATABASE_URI || ''}"
},
"options": {
"ssl": true
}
}
}
}
Ok, new plan.
try that :
npm install --save dotenv
require('dotenv').config()
{
"defaultConnection": "default",
"connections": {
"default": {
"connector": "mongoose",
"settings": {
"uri": `${process.env.DATABASE_URI || ''}`
},
"options": {
"ssl": true
}
}
}
}
I am new to programming and strapi. Just want to share i have problem with deploying to heroku with the same problem. I fix it with this:
{
"defaultConnection": "default",
"connections": {
"default": {
"connector": "mongoose",
"settings": {
"uri": "${process.env.DATABASE_URI}",
"database": "${process.env.DATABASE_NAME}"
},
"options": {
"ssl": true
}
}
}
}
If you are deploying to heroku please check the environment variables. When I set environment variables using heroku cli it does not save the whole string. I have to copy and paste to the heroku website directly.
Related
I currently have a set of nodejs modules (containers) that connect to a local mongodb (also a container).
All modules works perfectly but with one of the I have the following problem:
https://i.stack.imgur.com/xuFqw.png
The connection seems to be accepted by mongo but the client doesn't receive the response and keeps waiting until it times out after 30 second (watch timestamps in the log)
The code of the client is the following:
async function connectToMongo() {
try {
const mongoUrl = 'mongodb://mongo:27017/LocalDB'
let result = await MongoClient.connect(mongoUrl, { useUnifiedTopology: true })
logger.info("Connected to Mongo DB")
return result
} catch (err) {
logger.error("Cannot connect to Mongo - " + err)
throw err
}
}
And is exactly the same as the other modules (that works).
Mongo is started without any settings or authentication.
I have tried using the following hostnames:
mongo : name of the container in which mongo is running
host.docker.internal : to address the host since the 27017 port of the container is binded to the host port
I honestly don't know what to try anymore...does anybody have an idea about what could be the problem?
UPDATE: as requested i'm adding info about how i'm starting the containers. Anyway I have omitted this part because they are started as Azure IoT Edge Modules. Anyway here is the infos from the deployment template:
"modules": {
"mongo": {
"settings": {
"image": "mongodb:0.0.1-replset-noauth",
"createOptions": "{\"HostConfig\":
{\"PortBindings\":{\"27017/tcp\":[{\"HostPort\":\"27017\"}]}}}"
},
"type": "docker",
"version": "1.0",
"status": "running",
"restartPolicy": "always"
},
"healthcheck": {
"settings": {
"image": "${MODULES.HealthCheck.debug}",
"createOptions": {
"HostConfig": {
"PortBindings": {
"9229/tcp": [
{
"HostPort": "9233"
}
]
}
}
}
},
"type": "docker",
"version": "1.0",
"env": {
"MONGO_CONTAINER_NAME": {
"value": "mongo"
},
"MONGO_SERVER_PORT": {
"value": "27017"
},
"DB_NAME": {
"value": "LocalDB"
}
},
"status": "running",
"restartPolicy": "always"
}
},
While this is the configuration of another module that perfectly works:
"shovel": {
"settings": {
"image": "${MODULES.Shovel.debug}",
"createOptions": {
"HostConfig": {
"PortBindings": {
"9229/tcp": [
{
"HostPort": "9229"
}
]
}
}
}
},
"type": "docker",
"version": "1.0",
"env": {
"MONGO_CONTAINER_NAME": {
"value": "mongo"
},
"MONGO_SERVER_PORT": {
"value": "27017"
},
"DB_NAME": {
"value": "LocalDB"
}
},
"status": "running",
"restartPolicy": "always"
},
(it's basically the same)
The modules are currently being launched using the IoT Simulator or on an IoT Edge device as Single Device deployments.
Edit: I was able to get it to work partway, if I assumed that the 'local' variants could only overwrite already defined structures. That is, I need to define the data sources 'DynamoDB' and 'mongo' in datasources.json and override them in datasources.local.js. This, however, isn't what we want to do -- when we deploy to AWS, we don't want to have MongoDB involved in any way. Any help much appreciated!
We need to configure Loopback data sources and model configurations depending on a node env variable. Specifically, we need to switch between MongoDB as our DB for local development, and DynamoDB when it's deployed. I followed the instructions here, which say
Apparently, according to this post, I think one has to have the 'regular' datasources.json and model-config.json, and the other files can only add to what is defined there.
In any case, I added datasources.local.js and model-config.local.js but I still get the error. The environment variable LOOPBACK_ENV is being set correctly (to 'mongo' in this case) in the .env file
AssertionError [ERR_ASSERTION]: Event is referencing a dataSource that does not exist: "mongo"
at configureModel (/Users/abc/Documents/projects/abc-adj-new/node_modules/loopback/lib/application.js:584:5)
at Function.app.model (/Users/abc/Documents/projects/abc-adj-new/node_modules/loopback/lib/application.js:126:5)
at /Users/abc/Documents/projects/abc-adj-new/node_modules/loopback-boot/lib/executor.js:203:9
at Array.forEach (<anonymous>)
at setupModels (/Users/abc/Documents/projects/chatbot-adj-new/node_modules/loopback-boot/lib/executor.js:199:23)
at execute (/Users/abc/Documents/projects/abc-adj-new/node_modules/loopback-boot/lib/executor.js:40:3)
at bootLoopBackApp (/Users/abc/Documents/projects/abc-adj-new/node_modules/loopback-boot/index.js:154:3)
at Object.<anonymous> (/Users/abc/Documents/projects/abc-adj-new/server/server.js:79:1)
at Module._compile (internal/modules/cjs/loader.js:956:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
at Module.load (internal/modules/cjs/loader.js:812:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)
at internal/main/run_main_module.js:17:11 {
generatedMessage: false,
code: 'ERR_ASSERTION',
actual: false,
expected: true,
operator: '=='
}
I will add the files below. This is Loopback 3, Node 12.13. Any help much appreciated!
// server/datasources.json
{
"db": {
"name": "db",
"connector": "memory"
},
"emailDataSource": {
"name": "emailDataSource",
"connector": "mail",
"transports": [
{
"type": "smtp",
"host": "smtp-service",
"secure": false,
"port": 25,
"tls": {
"rejectUnauthorized": false
}
}
]
}
}
// server/datasources.local.js
if (process.env.LOOPBACK_ENV === "mongo") {
module.exports = {
"dynamodb": {
"name": "dynamodb",
"connector": "dynamodb",
"region": "ap-northeast-1",
"credentials": "shared",
"profile": "iamrole"
}
};
} else {
module.exports = {
"mongo": {
"url": "${MONGO_URL}",
"name": "mongo",
"connector": "mongodb",
"allowExtendedOperators": true,
"useNewUrlParser": true
}
};
}
// server/model-config.json
{
"_meta": {
"sources": [
"loopback/common/models",
"loopback/server/models",
"../common/models",
"./models",
"../node_modules/bot-analytics/models"
],
"mixins": [
"loopback/common/mixins",
"loopback/server/mixins",
"../common/mixins",
"./mixins",
"../node_modules/amf-lb-expose-remote-methods-mixin/lib"
]
},
"Variable": {
"dataSource": "db",
"public": true
}
}
// server/model-config.js
if (process.env.LOOPBACK_ENV === "mongo") {
module.exports ={
"Event": {
"dataSource": "mongo",
"public": true
},
"History": {
"dataSource": "mongo",
"public": true
},
"Email": {
"dataSource": "mongo"
},
"FileUpload": {
"dataSource": "mongo",
"public": true
} ,
"ADJUser" : {
"dataSource": "mongo"
}
}
} else {
module.exports = {
"Event": {
"dataSource": "dynamodb",
"public": true
},
"History": {
"dataSource": "dynamodb",
"public": true
},
"Email": {
"dataSource": "dynamodb"
},
"FileUpload": {
"dataSource": "dynamodb",
"public": true
} ,
"ADJUser" : {
"dataSource": "dynamodb"
}
}
}
Well, I'm not sure I will make a proper answer to your question - but here's what I usually do in my projects:
I have many files in the server folder depending on the configuration which serve different purposes depending on the configuration loaded. Usually like:
/server/middleware.json (for localhost)
/server/middleware.staging.json
/server/middleware.production.json
/server/datasources.json (for localhost)
/server/datasources.staging.json
/server/datasources.production.json
... and so on for each environments and files.
Then, depending on the environment, I launch the app with a specific command:
NODE_ENV=production npm start server/server.js
The NODE_ENV attribute will tell the app which configuration file to read. If you don't provide any, the first files will be read (in my example /server/middleware.json and /server/datasources.json)
Hope this helps!
I am using eureka-js-client to register on Eureka, this works fine when deployed locally, however when I deploy on the AWS, the "hostName" value set is replaced with empty value.
Since I set ingressUrl as hostName, how to stop eureka-js-client not to touch the "hostName" ?
eureka-js-client version - "4.2.0"
Eureka version - 1.9.2, (SpringBoot 2.x)
Here's portion of Eureka config looks like:
"instance": {
"app": "<MyApp>",
"hostName": "<MyHostname>", // I dont want this to be overwritten by AWS-meta-data
"ipAddr": "<MyIp>",
"vipAddress": "<MyVip>",
"secureVipAddress": "<address>",
"homePageUrl": "<url>",
"healthCheckUrl": "<url>",
"statusPageUrl": "<url>",
"instanceId": "<InstanceId>"
"port": {
"$": 80,
"#enabled": false
},
"securePort": {
"$": 443,
"#enabled": true
},
"dataCenterInfo": {
"#class": "com.netflix.appinfo.AmazonInfo",
"name": "Amazon"
},
},
"useLocalMetadata": true
}
This is what I end up doing,
"instance": {
"app": "<MyApp>",
"hostName": "<MyHostname>", // I dont want this to be overwritten by AWS-meta-data
"ipAddr": "<MyIp>",
"vipAddress": "<MyVip>",
"secureVipAddress": "<address>",
"homePageUrl": "<url>",
"healthCheckUrl": "<url>",
"statusPageUrl": "<url>",
"instanceId": "<InstanceId>"
"port": {
"$": 80,
"#enabled": false
},
"securePort": {
"$": 443,
"#enabled": true
},
"dataCenterInfo": {
"#class": "com.netflix.appinfo.AmazonInfo",
"name": "Amazon"
},
"eureka": {
"serviceUrls": {
"default": "<eureka_url>"
},
"fetchMetadata" : false, #turn this off, and set your own metadata, you can use AWSmetada.js present in eurek-client-js
"registerWithEureka" : true
}
}
for more details see here: https://www.npmjs.com/package/eureka-js-client#configuring-for-aws-environments
I'm trying to deploy strapi on google cloud app engine (standard env) but I keep getting a 500 server error. I googled all over but no guides have been written on how to successfully deploy strapi on AE.
I tried the suggestions on this thread: https://github.com/strapi/strapi/issues/2146
So I have:
the gcp-build script to install dependencies
an entrypoint in app.yaml to start strapi
my database.json and server.json in strapi are updated
But it does not seem to work for me. I keep getting the following error:
I tried googling that error "app/invalid" but I can't seem to find anything about that.
I also gave the flex env a spin but that failed as well (without a proper error).
This is my app.yaml file:
runtime: nodejs10
instance_class: F2
service: admin
entrypoint: node_modules/strapi/bin/strapi.js
env_variables:
DATABASE_HOST: "host"
DATABASE_PORT: 27017
DATABASE_NAME: "db"
DATABASE_USERNAME: "name"
DATABASE_PASSWORD: "pw"
DATABASE_SRV: true
DATABASE_AUTHENTICATION_DATABASE: "admin"
DATABASE_SSL: true
NODE_ENV: "production"
PORT: 1337
This is my package.json (important parts):
...
"scripts": {
"strapi": "node_modules/strapi/bin/strapi.js",
"gcp-build": "node node_modules/strapi/lib/utils/post-install.js && cd admin && npm run setup"
}
...
"engines": {
"node": ">=10.0.0",
"npm": ">=6.0.0"
},
production database.json:
{
"defaultConnection": "default",
"connections": {
"default": {
"connector": "strapi-hook-mongoose",
"settings": {
"client": "mongo",
"host": "host",
"port": 27017,
"database": "db",
"username": "name",
"password": "pw",
"srv": true
},
"options": {
"authenticationDatabase": "admin",
"ssl": true
}
}
}
}
And finally this is my server.json file:
{
"host": "https://admin-dot-ootje-website.appspot.com",
"port": 1337,
"production": true,
"proxy": {
"enabled": false
},
"autoReload": {
"enabled": false
},
"cron": {
"enabled": false
},
"admin": {
"autoOpen": false
}
}
I would expect that this works from the getting started guides in AE but it would seem I'm missing something. Does someone know what that error means? Did someone manage to already deploy strapi on AE?
If I get it working I'd like to add it to strapi docs or medium post for other people to find it easier than I did :)
Thanks in advance!
I think your problem provide from server.json file.
This configuration works on my side :
{
"host": "localhost",
"port": "${process.env.PORT || 1337}",
"production": true,
"proxy": {
"enabled": false
},
"autoReload": {
"enabled": false
},
"cron": {
"enabled": false
},
"admin": {
"autoOpen": false
}
}
I'm working on a new configuration file in order to have the auth provider working... and some other features. But this first version is working for me.
I keep you update of my search if you need.
## Update 1 ##
I found the good configuration for GCP in production mode.
I share it if anyone else need it :
{
"host": "localhost",
"port": "${process.env.PORT || 1337}",
"production": true,
"proxy": {
"enabled": true,
"ssl": true,
"host": "[project-name].appspot.com",
"port": 443
},
"autoReload": {
"enabled": false
},
"cron": {
"enabled": false
},
"admin": {
"autoOpen": false
}
}
I use flexible environment on GCP, but this will work in standard mode I think.
Thanks,
I just got started using strapi framework and I would like to use mLab as my mongoDB database, so I go to configure strapi and fill the following details:
{
"defaultConnection": "default",
"connections": {
"default": {
"connector": "strapi-mongoose",
"settings": {
"client": "mongo",
"host": "localhost",
"port": 27017,
"database": "development",
"username": "",
"password": ""
},
"options": {}
}
}
}
The details I get from mLab are:
mongodb://myUsername:myPassword#ds047891.mlab.com:41365/myDatabase
Here is my final config:
{
"defaultConnection": "default",
"connections": {
"default": {
"connector": "strapi-mongoose",
"settings": {
"client": "mongo",
"host": "ds047891.mlab.com",
"port": 41365,
"database": "myDatabase",
"username": "myUsername",
"password": "myPassword"
},
"options": {}
}
}
}
When I try to start strapi, I get the following error:
DEBUG (2748 on DESKTOP-HAL1ATE): Server wasn't able to start properly.
ERROR (2748 on DESKTOP-HAL1ATE): (hook:mongoose) takes too long to load
I think that I did not setup my configuration right, but I can't pinpoint where the problem is. I hope someone could, thanks.
I am Pierre, on of the creators of Strapi. I tried with the following configuration and it worked well:
{
"defaultConnection": "default",
"connections": {
"default": {
"connector": "strapi-mongoose",
"settings": {
"client": "mongo",
"host": "ds135777.mlab.com",
"port": "35777",
"database": "myDatabase",
"username": "myUsername",
"password": "myPassword"
},
"options": {}
}
}
}
Our configurations files look quiet similar.
What file did you updated (/config/environment/development/database.json or /config/environment/production/database.json)?
Are you sure you entered the correct username and password? Did you try to login to your MongoDB instance through the command line mongo ds135777.mlab.com:35777/myDatabase -u <dbuser> -p <dbpassword>?
UPDATE
In version >= 3 for mlab don't forget to specify authenticationDatabase
"options": {
"authenticationDatabase": "your_mlad_database_name",
"ssl": false
}