Node js code works in local machine but not working in Apigee.

Not applicable

The below code works in local machine when executed by Node.js module. But its throwin error "Cannot GET/" when run in Apigee cloud. Pls help.

var request = require('request');
var express = require('express');
var async = require('async');
var app = express();
var output,jsonRes1;


 async.parallel([
 function(callback) {
	request('https://api.usergrid.com', function (error, response, body) {
		if (!error && response.statusCode == 200) {
			jsonRes1 = JSON.parse(body);
		}
	});
 },	function(callback) {
	setTimeout(function() {
  request('https://api.usergrid.com', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    var jsonRes2 = JSON.parse(body);
	 output = {'Output':{'Response1':{'Time':jsonRes1.timestamp,'Period':jsonRes1.duration}},'Response2':{'Start':jsonRes2.status.started,'CountryCode':jsonRes2.status.uptime}};
    console.dir(output);   
  }
});
	},2000);
	}
],
 function(error, results) {
  console.log(results);
});


app.listen(8001,function (req,res) {
  console.log('Server running at http://198.1.2.3:8888/');
     });
0 8 500
8 REPLIES 8

Dear @Ranjith Rajendran ,

I think it's issue with port you are listening,

Can you try below code ?

var port = process.env.PORT || 8888
app.listen(port);

@Anil Sagar

I tried the code you suggested. Still facing the same issue.

Based on the code I see, there are no route handlers registered. There is no `app.get` or anything like that. Since there are no route handlers, Express gives you a `404`. Here's an example: http://expressjs.com/en/starter/hello-world.html

Not applicable
"use strict";
var express = require('express');
var bodyParser = require('body-parser');
var session = require('express-session');
var PORT = process.env.PORT || 9000;
var rp = require('request-promise');
var app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(session({
    secret: 'this-should-be-secure',
    resave: true,
    saveUninitialized: true,
    cookie: {}
}));


app.use(function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next();
});

app.get('/', function (req, res) {
    res.send('Hello world');
});
app.get('/posts',function (req, res) {
    var options = {
        uri: 'https://jsonplaceholder.typicode.com/posts',
        headers: {
            'User-Agent': 'Request-Promise'
        },
        json: true
    };
    rp.get(options).then( function(postsresponse){
        res.send(postsresponse);
    }).catch(function(err) {
        console.log(err);
        res.send(err);
    })
});
app.get('/posts/:postsId', function(req, res){
    var postsId = req.params.postsId;
    console.log(postsId);
    rp.get('https://jsonplaceholder.typicode.com/posts/' + postsId).then(function(postsresponse) {
        res.send(postsresponse);
    }).catch(function(err){
        res.send(err);
    })
});
app.post('/posts', function(req, res) {
    var title = req.body.title;
    var body  = req.body.body;
    var userId= req.body.userId;
    rp.post('https://jsonplaceholder.typicode.com/posts', {
        method: 'POST',
        body: JSON.stringify({
            title: title,
            body: body,
            userId: userId
        }),
        headers: {
            "Content-type": "application/json; charset=UTF-8"
        }
    }).then(function(responsePosts){
        res.send(responsePosts);
    }).catch(function (error) {
            console.log(error);
            res.send(error);
    })
});

app.listen(PORT);
console.log('Running on posrt : '+ PORT);

/*This is my current updated code. Please corrent if I am doing something wrong.*/

/*Exception */

{"fault":{"faultstring":"Script node executed prematurely: InternalError: syntax error (\/organization\/environment\/api\/node_modules\/hoek\/lib\/index.js#371)\nInternalError: syntax error (\/organization\/environment\/api\/node_modules\/hoek\/lib\/index.js#371)\n    at \/organization\/environment\/api\/node_modules\/request-promise\/lib\/rp.js:23\n    at module.js:456\n    at module.js:474\n    at module.js:356\n    at module.js:312\n    at module.js:364\n    at require (module.js:380)\n    at \/organization\/environment\/api\/index.js:6\n    at module.js:456\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"}}}

Not applicable

Hi Giys,

I am getting below exception :

{"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:364\n    at require (module.js:380)\n    at \/organization\/environment\/api\/node_modules\/request\/node_modules\/hawk\/node_modules\/boom\/lib\/index.js:5\n    at module.js:456\n    at module.js:474\n    at module.js:356\n    at module.js:312\n    at module.js:364\n    at require (module.js:380)\n    at \/organization\/environment\/api\/node_modules\/request\/node_modules\/hawk\/lib\/index.js:5\n    at module.js:456\n    at module.js:474\n    at module.js:356\n    at module.js:312\n    at module.js:364\n    at require (module.js:380)\n    at \/organization\/environment\/api\/node_modules\/request\/request.js:9\n    at module.js:456\n    at module.js:474\n    at module.js:356\n    at module.js:312\n    at module.js:364\n    at require (module.js:380)\n    at \/organization\/environment\/api\/node_modules\/request\/index.js:143\n    at module.js:456\n    at module.js:474\n    at module.js:356\n    at module.js:312\n    at module.js:364\n    at require (module.js:380)\n    at \/organization\/environment\/api\/app.js:4\n    at module.js:456\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"}}}

Please help me with this code. It's working in local but not on apigee edge :

const express = require('express');
const bodyParser = require('body-parser');
const session = require('express-session');
const requestUrl = require('request');




const app = express();
const PORT = process.env.PORT || 9000;


app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(session({
    secret: 'this-should-be-secure',
    resave: true,
    saveUninitialized: true,
    cookie: {}
}));
app.use(function (request, response, next) {
    response.header("Access-Control-Allow-Origin", "*");
    response.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next();
});
app.get('/', function (request, response) {
    response.send('Hello world');
});
app.get('/posts', function (request, response) {
    const options = {
        uri: 'https://jsonplaceholder.typicode.com/posts',
        headers: {
            'User-Agent': 'Request-Promise'
        },
        json: true
    };
    requestUrl.get(options, function (err, ds) {
        if (err) {
            response.send(err);
        } else {
            response.send(ds);
        }
    })
});
app.get('/posts/:postsId', function (request, response) {
    const postsId = request.params.postsId;
    // console.log(postsId);
    requestUrl.get('https://jsonplaceholder.typicode.com/posts/' + postsId).then(function (postsresponse) {
        response.send(postsresponse);
    }).catch(function (err) {
        response.send(err);
    })
});
app.post('/posts', function (request, response) {
    const title = request.body.title;
    const comment = request.body.comment;
    const userId = request.body.userId;
    requestUrl.post('https://jsonplaceholder.typicode.com/posts', {
        method: 'POST',
        body: JSON.stringify({
            title: title,
            body: comment,
            userId: userId
        }),
        headers: {
            "Content-type": "application/json; charset=UTF-8"
        }
    }, function (err, ds) {
        if (err) {
            response.send(err);
        } else {
            response.send(ds.body);
        }
    })
});


app.listen(PORT, function () {
    console.log('The server is listening on port 9000');
});

You really should ask a separate question instead of highjacking someone else's thread. As I've answered a few times now, you are attempting to use ES6 syntax in a Node.js runtime that does not support it. Please change your JS syntax to be ES5 and everything should work fine.

Sorry Jeremy. I will not repeat it again. I will raise new question

I remove all ES6 code still getting same error.