Connect Mongoose to Google App Engine

I have successfully applied the Node.js custom application to the Google App application engine. However, I am having problems starting the application, because the mongoose time when trying to connect. Frustratedly, the mongoose connects very simply to my local machine with the same parameters.

My MongoDb URI has the following form:

 mongodb://<dbuser>:<dbpassword>@xxxx.mlab.com:<portNumber>/dā€Œā€‹b-name

As follows from the URI, db is hosted using mlab. This is a sandbox instance on the Google Cloud Platform. However, I did not use db with the Google Compute Engine. I just decided to host on GCP while going through the installation process on mlab.

I came across similar questions on SO, but most of them do not have an accepted answer. Just a different discourse in the comments without a decision.

So my question is, what is different from my App Engine instance and my local computer when trying to connect to the above URI? Is the fact that I chose for mlab to host it on GCP? Would it be different if I chose to host it on Amazon AWS? What is the main cause of the problem?

For reference, here are the similar questions I found:

Also, if that helps, mongodb is used as db for business models, a data store for an Express Session application, and for storing temporary data from socket.io.

stacktrace App Engine :

2017-10-18 02:13:46 default[20171017t215757]  npm ERR! enoent ENOENT: no such file or directory, open '/app/package.json'
2017-10-18 02:13:46 default[20171017t215757]  npm ERR! enoent ENOENT: no such file or directory, open '/app/package.json'
2017-10-18 02:13:46 default[20171017t215757]  npm ERR! enoent This is most likely not a problem with npm itself
2017-10-18 02:13:46 default[20171017t215757]  npm ERR! enoent and is related to npm not being able to find a file.
2017-10-18 02:13:46 default[20171017t215757]  npm ERR! enoent
2017-10-18 02:13:46 default[20171017t215757]
2017-10-18 02:13:46 default[20171017t215757]  npm ERR! Please include the following file with any support request:
2017-10-18 02:13:46 default[20171017t215757]  npm ERR!     /app/npm-debug.log
2017-10-18 02:23:41 default[20171017t215757]  npm ERR! Linux 3.16.0-4-amd64
2017-10-18 02:23:41 default[20171017t215757]  npm ERR! argv "/nodejs/bin/node" "/nodejs/bin/npm" "start"
2017-10-18 02:23:41 default[20171017t215757]  npm ERR! node v6.11.3
2017-10-18 02:23:41 default[20171017t215757]  npm ERR! npm  v3.10.10
2017-10-18 02:23:41 default[20171017t215757]  npm ERR! path /app/package.json
2017-10-18 02:23:41 default[20171017t215757]  npm ERR! code ENOENT
2017-10-18 02:23:41 default[20171017t215757]  npm ERR! errno -2
2017-10-18 02:23:41 default[20171017t215757]  npm ERR! syscall open

stacktrace Google:

/home/myuser/src/project-id/teammate-express-server/node_modules/mongodb/lib/mongo_client.js:421
          throw err
          ^
MongoError: failed to connect to server [ds147454.mlab.com:47454] on first connect [MongoError: connection 1 to ds147454.mlab.com:47454 timed out]
    at Pool.<anonymous> (/home/myuser/src/project-id/teammate-express-server/node_modules/mongodb-core/lib/topologies/server.js:336:35)
    at emitOne (events.js:96:13)
    at Pool.emit (events.js:188:7)
    at Connection.<anonymous> (/home/myuser/src/project-id/teammate-express-server/node_modules/mongodb-core/lib/connection/pool.js:280:12)
    at Connection.g (events.js:292:16)
    at emitTwo (events.js:106:13)
    at Connection.emit (events.js:191:7)
    at Socket.<anonymous> (/home/myuser/src/project-id/teammate-express-server/node_modules/mongodb-core/lib/connection/connection.js:197:10)
    at Socket.g (events.js:292:16)
    at emitNone (events.js:86:13)
    at Socket.emit (events.js:185:7)
    at Socket._onTimeout (net.js:338:8)
    at ontimeout (timers.js:386:14)
    at tryOnTimeout (timers.js:250:5)
    at Timer.listOnTimeout (timers.js:214:5)
+5
3

, , Docker.

node.js, Dockerfile , npm.

Docker, :

gcloud beta app gen-config --custom

node.js .

Docker :

. MongoDb . Cloud Shell .

# Dockerfile extending the generic Node image with application files for a
# single application.
FROM gcr.io/google_appengine/nodejs
COPY . /app/

RUN apt-get -y update && apt-get install -y libav-tools && apt-get install -y graphicsmagick

# You have to specify "--unsafe-perm" with npm install
# when running as root.  Failing to do this can cause
# install to appear to succeed even if a preinstall
# script fails, and may have other adverse consequences
# as well.
# This command will also cat the npm-debug.log file after the
# build, if it exists.
RUN npm install --unsafe-perm || \
  ((if [ -f npm-debug.log ]; then \
      cat npm-debug.log; \
    fi) && false)
CMD npm start
+2

, https://cloud.google.com/nodejs/getting-started/deploy-mongodb Google App Engine config.json. (: , : https://cloud.google.com/community/tutorials/nodejs-mongodb-on-appengine )

mongoose . .. (), (). node.js, / .

App Engine URI?

Google App Engine - . - , / . , /.

, .

Google App Engine.

, mlab GCP?

. , SandBox GCP. MongoDB us-central1, . RAM/Storage/Price .

, GAE us-central1 us-central1.

?

, , , -.

node.js JavaScript config.js :

  nconf.argv()
       .env()
       .file({ file: path.join(__dirname, 'config.json') })
       .defaults()

:

  • ( )

Google Cloud Platform: nodejs-Getting-Start

GoogleCloudPlatform/nodejs-docs-samples/appengine/mongodb .

+5

, . , . db json, nconf, google https://cloud.google.com/community/tutorials/nodejs-mongodb-on-appengine mongoose . env.

'         nconf = require('nconf');
          const mongoose = require('mongoose');
          //key.json contains mongodb connection information
          nconf.argv().env().file('keys.json');
          const user = nconf.get('mongoUser');
          const pass = nconf.get('mongoPass');
          const host = nconf.get('mongoHost');
          const port = nconf.get('mongoPort')
          const mongoDatabase=nconf.get('mongoDatabase');
          let mongo_url='mongodb://${user}:${pass}@${host}:${port}/${mongoDatabase}';
          mongoose.connect(mongo_url,{useNewUrlParser: true});'

config.json, .

0
source

Source: https://habr.com/ru/post/1686759/


All Articles