NodeJS client connection to Datastore

25 views
Skip to first unread message

Giancarlo C.

unread,
Jan 15, 2018, 7:24:00 PM1/15/18
to Google App Engine
Hi everybody, I'm new in this kind of platform (cloud) and I have a problem with App Engine.

I have the following project structure in App Engine:

- Root
  - nodejs-docs-samples
  - src
    - be-nodejs-piloto
       - node_modules
       - api.js (api post uri)
       - app.js (main)
       - app.yaml
       - datastore-quickstart.json
       - package-lock.json
       - package.json
    - vibrant-tree-191664


app.yaml (content)
runtime: nodejs
env: flex

manual_scaling:
  instances: 1
resources:
  cpu: 1
  memory_gb: 0.5
  disk_size_gb: 10

api.js (content)
'use strict';

// Load libs.
var express  = require('express');
var router   = express.Router();
var path = require('path');

const Datastore = require('@google-cloud/datastore'); // Imports the Google Cloud client lib

// Your Google Cloud Platform project ID
const projectId   = 'datastore-quickstart-191515';
const keyFilename = '/home/testcloud99/src/be-nodejs-piloto/datastore-quickstart-5672f2cde8ca.json';

console.log('keyFilename:' + keyFilename);

// Creates a client
const datastore = new Datastore({
    projectId: projectId,
    keyFilename: keyFilename
  });


router.route('/api/piloto')

    .post(function (req, res)
    {
    console.log('method: POST');

        // Read params
        var pMsgId   = req.body.msgId;

        const query = datastore.createQuery('MyEntity');
        query.filter('msgId', '=', pMsgId);

        // exec query
        datastore
            .runQuery(query)
            .then(results => {

                //OK
                return res.status(200).jsonp({
                    "piloto":
                    {
                        "code" : 0,
                        "desc" : "ok",
                    }
                });

        })
        .catch(err => {
            console.error('ERROR:', err);
            return res.status(200).jsonp({
                "piloto":
                {
                    "code" : 1,
                    "desc" : "error",
                    "errorMessage" : err.message
                }
                });
        });

    });

module.exports = router;

So, when I send a POST message (using soapUI), I get this response:

{"piloto": {
   "code": 1,
   "desc": "error",
   "errorMessage": "ENOENT: no such file or directory, open '/home/megagecs/src/be-nodejs-piloto/datastore-quickstart-5672f2cde8ca.json'"
}}

I guess App Engine isn't recognizing that JSON file but I don't know why. Any kind of configuration that should be done?

Hope you can help me.

Regards.

Reply all
Reply to author
Forward
0 new messages