Node app fails

Not applicable

Altough this code works, from my local, as sson as I deploy it to Edge, it causes me issue,

Can somone help.

Thanks

Package.json

{
  "name": "baas",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": {
    "name": "XXX",
    "email": "xxxx@xxxxx.com"
  },
  "dependencies": {
    "body-parser": "^1.18.2",
    "cors": "^2.8.4",
    "express": "^4.16.2",
    "usergrid": "^2.0.0-rc.2"
  }
}


Code

var express = require('express');
var bodyParser = require('body-parser')
var UsergridClient = require('./node_modules/usergrid/lib/client')
var UsergridQuery = require('./node_modules/usergrid/lib/query')


// var cors =require('cors')
/*Set up Express environment and enable it to read and write JavaScript.*/
var app = express()
// app.all(cors())
app.use(bodyParser.json())


app.use(bodyParser.urlencoded({
  extended: true
}))


var config = require('./config');
// Initialize Usergrid

var Usergrid = new UsergridClient({
	'orgId' : config.organization,
	'appId' : config.application,
	'clientId' : config.clientId,
	'clientSecret' : config.clientSecret,
    "authMode": "UsergridAuth.AUTH_CLIENT_ID",
    "baseUrl": "https://baas-ug002sr.apigee.net",
     Url: "https://baas-address.apigee.net",
	logging : config.logging
});




/*Authenticate user*/
Usergrid.authenticateApp(function(err, usergridResponse) {
    if (usergridResponse.ok) {
        console.log('app is now authenticated')
    }
})


// GET /
var rootTemplate = {
	'employees' : {
		'href' : './employees'
	}
};


app.get('/', function(req, resp) {
	resp.jsonp(rootTemplate);
});


// GET /profiles


app.get('/profiles', function(req, res) {	
		getProfiles(req, res);
});


function getProfiles(req, res) {
	Usergrid.GET('/profile', function(error, usergridResponse) {
    if (error) {
      res.jsonp(500, {
        'error' : JSON.stringify(error)
      });
      return;
    }
    var entities = usergridResponse.entities;
    //console.log('entities: ' + JSON.stringify(entities, null, 2));
    var emps = entities.map( (emp) => { return { entity: emp.accountA }});
    res.jsonp(emps);
  });
}




app.post('/profile', function(req, res) {


 if (!req.is('json')) {
   res.jsonp(400, {
     error : 'Bad request'
   });
   return;
 }


  var payload = req.body;
  var entity = {
    type: 'restaurant',
    restaurant: payload.restaurant,
    cuisine: payload.cuisine
  }


  if ((entity.restaurant === undefined) || ( entity.cuisine === undefined) ) {
    res.jsonp(400, {
      error : 'Bad request'
    });
    return;
  }


  Usergrid.POST(entity, function(error, usergridResponse, entity) {
    // entity should now have a uuid property and be created
        res.send(201);
  });
});


// Listen for requests until the server is stopped
app.listen(process.env.PORT || 9000);
console.log('The server is running!');




Error

{
	"fault": {
		"faultstring": "Script node executed prematurely: syntax error\nsyntax error\n    at module.js:439\n    at module.js:474\n    at module.js:356\n    at module.js:312\n    at module.js:497\n    at startup (trireme.js:142)\n    at trireme.js:923\n",
		"detail": {
			"errorcode": "scripts.node.runtime.ScriptExitedError"
		}
	}
}
0 3 393
3 REPLIES 3

You might be experiencing a trireme incompatibility.

It's possible the usergrid2.0 JS client is not compatible with trireme.

In case you did not know, trireme is the host for nodejs apps in Apigee Edge. If you run a nodejs app within Apigee Edge, today, then you are using Trireme. It's a JS engine that runs within the JVM. You can learn more about it on the project's github homepage.

Trireme is pretty neat, but there are some npm modules that rely on v8 being the host, and those modules won't run within trireme. The v2.0 JS client for Usergrid has been refactored significantly from the original 0.10 version of the client. This refactoring may have introduced a new dependency on a module that is not compatible with trireme.

I've reproduced the problem you reported, so it's not just you.

The workaround is to use the usergrid v0.10.x version of the JS client . OR, just use the request module directly; usergrid is just an HTTP endpoint.

I'll investigate a little more to see if I can more narrowly pinpoint what the problem is.

Not applicable

By the way,

How do I access the full path to where all the Modules are installed?

varUsergridClient=require('./node_modules/usergrid/lib/client')varUsergridQuery=require('./node_modules/usergrid/lib/query')

Not applicable

@Dino Thank a lot, I will use request module for the time been

Thanks