setting up graphQL on OpenShift - postgresql

I'm learning how to set up a server on OpenShift that uses node, express, GraphQL and Postgres and I need some help.
When trying to set up the GraphQL Server I've been following this and it works on my local machine:
import Express from 'express';
import GraphQL from 'express-graphql';
import Schema from './db/GQLschema';
const app = Express();
app.use('/graphql', GraphQL({
schema: Schema,
pretty: true,
graphiql: true
}));
...
I'm using the server.js template provided by OpenShift so the above changes to:
...
self.app = express();
self.app.configure(function() {
self.app.use('/public', express.static(__dirname+'/public'));
self.app.use('/graphql', GraphQL({
schema: Schema,
pretty: true,
graphiql: true
}));
});
...
But this doesn't work when pushed up to OpenShift. I get 'Internal Server Error' instead of the graphiql interface.
I'm new to this, but here are some guesses as to what it might have to do with:
Connecting to database
Missing dependencies
Connecting to database: I have 2 json config files. One for my local machine which is connected using a port-forward SSH tunnel to OpenShift. That works fine. The other is one for production. In it I have all the strings for the database name, user, port, host, and password, that OpenShift provided me. I'm using the config library which looks at NODE_ENV to decide which config json file to use. I set the NODE_ENV variable on OpenShift to production. When I query it, it shows all the correct strings. Is there something else I need to set?
Missing dependencies: To keep it neat, I am keeping my back-end code separate from my front-end code. So the back-end code has only the express, graphql, sequelize, config, and pg stuff installed. Do I need something else to make the graphiql page work? Also, I put all the /node_modules/ in the .gitignore file -- I assumed that OpenShift installs them post push. Was that the right thing to do?
Would appreciate any help!

OK - I solved it by doing the following:
in the openshift cli tools turn on error logs
rhc tail -a <yourappname>
This will give you some clues. In my case it was a message from express-graphql "unresolved promise". After researching I found that if you install and save es-promise and add this line to the top of the server.js file:
require ('es6-promise').polyfill();
Hope that helps someone else!

Related

How to change localhost to MongoDb and Heroku host

so i finish to build my app with mongodb express react native with expo ,
and im trying to change the local host server with port 3000, to real host server with horuko and mongodb.
i was build and connected the mongoDb to heroku like the docs, i go to settings in heroku and paste the momngodb_uri , something like :
mongodb+srv://admin:admin#lior.rva16.mongodb.net/<dbname>?retryWrites=true&w=majority
in my Index server.js i change from local to :
const port = process.env.PORT;// here i dont really have port (i dont know what to put inside)
mongoose
.connect(process.env.MONGO_URI, {///this line is -
>mongodb+srv://admin:admin#lior.rva16.mongodb.net/<dbname>?retryWrites=true&w=majority
useNewUrlParser: true,
useUnifiedTopology: true,
})
this is my env file:
MONGO_URI=mongodb+srv://admin:admin#lior.rva16.mongodb.net/<dbname>?retryWrites=true&w=majority
JWT_SECRET=LKDSLCX9IOZiJS9IAJ768
in my client side all the request url is like:
await indexApi.post("/signout");
and until now i used with NGROK to connect and check my db so the base url until now is been:
export default axios.create({
baseURL: "http://cca1237fb2de.ngrok.io", //ngrok http 3000
});
the problem:
i get errors that all of the is show that is cant connect to the url, he dont find the server url.
(note that with the localhost everything worked correctly)
the question:
how can i connect my app to heroku server
what is my new BaseUrl ? (im tried to do {process.env.MongoDB_Uri} but is also crash...)
the big question is "how can i replace the server from local to real server in heroku with mongo"
lin https://github.com/roeigr7/LIOR
The best way to do this is to have an environment variable that stores your production/staging MONGO_URL. On your local machine you can use dotenv package to pull values from .env files and the environment variables you set on your heroku app deployment.
Local .env
MONGO_URL=mymongourl
You can set environment variables in your heroku app by going to Settings > Config Vars. There should be a button named Reveal Config Vars. Click on that and it will give you an interface to add more environment variables.
Code
const dotenv = require('dotenv')
dotenv.config()
...other code
const mongoURL = process.env.MONGO_URL

node pg.connect never calling its callback, how to debug?

So I have a working express server talking to a postgres backend running on a heroku server. I'm trying to move to AWS. On AWS I think I have everything set up correctly. I have my express server running (under PM2 though that shouldn't make a difference. I can SSH into the ec2 and from there launch a psql and connect to the DB without any issues, I've created my schema no problem.
However my app is not working with the DB, Most likely its not even connecting but I'm getting nothing back. not even a timeout. I first tried with env variables to set everything up but decided to put the user/password in the code.
my express code looks like this obviously i changed the values for the post but they are the same values
I use on the psql command line
const {Client} = require('pg');
const db = new Client({
user: 'myUserName',
host: 'myHostName',
password: 'myPassword',
});
console.log("about to connect to the DB");
db.connect(err => {
if (err) {
console.error('connection error', err.stack)
} else {
console.log('connected')
}
}
);
In my logs I get the line "about to connect to the DB" but I never get anything else I do not
get the error nor the success line.
how do I see what's wrong? with no feedback I'm stuck. Is there something I can check in aws?
I doubt its a security group issue since psql works
Seems I had a bad version/installation of Node. I created a new instance on AWS and installed Node 12.x instead of "latest" which was 14.x and its working fine now.

Problems with installing web based Dashboard (DM Dashboard) with Git, MongoDB, Node, mean.io, Forever

I was trying to get the DM Dashboard working on my Ubuntu 16.04. You can find it here: https://github.com/dm-dashboard/dashboard
I already installed Git, MongoDB, Node, Mean.io and Forever. I'm new to working with all of the things, including Git. There are two things I don't understand:
In the instructions you can find this:
Before we start the server, we will need to configure it for your environment
To Configure
Change to the checkout folder
Open config/env/development.json
Update the "db" property to point to your mongo DB
Save and close the file
When opening the development.js the part where "db" is foud says:
module.exports = {
db: 'mongodb://' + (process.env.DB_PORT_27017_TCP_ADDR || 'localhost') + '/dashboard',
debug: true,
// aggregate: 'whatever that is not false, because boolean false value turns aggregation off', //false
aggregate: false,
mongoose: {
debug: false
},
Now what do I have to change to what? I don't know where my MongoDB is.
To open the Dashboard in a browser the instruction says:
Open your browser and navigate to
http://[path_to_where_you_deployed]:3000
What does "path_where_you_deployed" mean?
I feel like the solution is very simple, but I just can't figure out what to do.
I really appreciate you help!
db should be your Mongo DB URI
[path_to_where_you_deployed] will we where you spin up this server, such as 127.0.0.1 if you're running locally for dev... or a heroku url if you deploy there

Unable to connect to mLab database from self-hosted Parse

TL;DR: I can get my parse dashboard talking to my locally-hosted Parse server and mongo db instance but cannot get the parse server to talk to the mLab-hosted database.
I am going through the Parse migration guide and have got mongo DB, parse-server-example and parse-dashboard running locally. When I use the following details in the parse index.js file I can successfully connect the dashboard and see the test items in the database:
databaseURI: 'mongodb://localhost:27017/dev',
cloud: __dirname + '/cloud/main.js',
appId: '1',
masterKey: '1',
serverURL: 'http://localhost:1337/parse'
I have installed mongo db locally and when connecting to my mLab instance with the shell I can see the database content. When I use that same mLab connection string in the databaseURI parameter within index.js the dashboard can no longer see the database content and the /test page on the locally-hosted parse server.
The Parse Migration Guide states...
Go to the Security & Keys section of App Settings in your Dashboard
and take note of the File Key and Master Key values. Pass that into
the ParseServer constructor in index.js. You no longer need to use a
client key with Parse Server.
I can find those keys but I cannot see where to put the File Key into the index.js.
I also do not understand why those keys are required if the locally-hosted Parse server and mLab database know nothing about them.
steps :
create your user/pwd in the mLab/mongo instance
get the db URL from mLab dashboard
connect using a command lib client to verify what parse-server will
use. this verifies the user/pwd you will use below...
go back to 'parse-server.js' to config it for mongo/remote
var databaseUri = $what-was-on-cli-client-above
var api = new ParseServer({
databaseURI: databaseUri || 'mongodb://<db.....
....

Node.js - Connecting to MongoDB using MongoHQ on Heroku

I have a Node.js app using Express that stores data in a mongoDB (locally).
It's now pushed to heroku successfully on a cedar stack and the server is running. I added the mongohq addon through the terminal.
Now... how do I connect that mongoDB through mongohq to my application to start using it??? I don't see a tutorial for this anywhere online. If you can point me to one or get me started on where to add the configuration, I would greatly appreciate!
Thanks much.
Update:
I've tried the following (with real values for MYPASSWORD and MYDBNUMBER:
in routes.js
var db = mongoose.connect('mongodb://heroku:<MYPASSWORD>#staff.mongohq.com:10049/<MYDBNUMBER>');
in my schema.js (also tried using the heroku
var mongoose = require('mongoose');
mongoose.connect('mongodb://heroku:<MYPASSWORD>#staff.mongohq.com:10049/<MYDBNUMBER>');
my package.json
{
"name": "NAME"
, "version": "0.0.1"
, "dependencies": {
"express": "2.4.6"
, "connect": "1.7.1"
, "stylus": ">= 0.0.1"
, "mongodb": ">= 0.9.6-7"
, "mongoose": ">= 2.0.0"
, "ejs": ">=0.4.3"
}
}
Right now, only the root '/' GET is successful. Actually, if I try /page/ANYTHING I can successfully get a rendered 500 Error page that I made to handle attempts to get 'pages' that haven't been created... That seems kind of weird to me. Everything else gives me an Internal Server Error.
ALSO, if i go to mongohq and open my database there, a collection for my model pages has been created and the indexes for uniqueness have been created, even tho I haven't been able to access the pages to create or view the model in my app...
happy to provide any other info... so stuck.
We are using heroku against MongoHQ but in our case we created the MongoHQ account on our own so our solution is a bit different.
Now, we are using the Redis plugin and in that case we connect to Redis using the heroku env variables.
When you create a new plugin, Heroku adds a bunch of env variables that you can access from your node.js app easily doing this:
process.env.NAME_OF_VAR
In the case of Redis to go the name of the var is REDISTOGO_URL so our code looks like
process.env.REDISTOGO_URL
For MongoHQ the env variable seems to be MONGOHQ_URL
You may need to do some parsing. Heroku have some documentation for Ruby here http://devcenter.heroku.com/articles/mongohq#using_the_mongo_ruby_driver that should help you to get it going.
With respect to where to do this in an express app. We are doing all the setup on app.js that is our entry point in the app.
Hope this helps.