request module not working with apigee tough it working in local

Not applicable

Exception Details: I written code in local this code in working on local system. It doesn't have any ES6 feature.

{"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"}}}
var express = require('express');
var bodyParser = require('body-parser');
var session = require('express-session');
var requestUrl = require('request');


var app = express();
var 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) {
    var 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) {
    var 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) {
    var title = request.body.title;
    var comment = request.body.comment;
    var 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');
});<br>
0 3 180
3 REPLIES 3

ylesyuk
Participant V

Hi @Tushar Budhe

I took your code, 'installed' it, and deployed it on a Cloud Edge trial account... successfully.

No errors, it works and returns me the correct answers for / and /posts.

You would need to be more specific under which conditions your code does not work.

Here is dependency versions I am using:


  "dependencies": {

    "express": "^4.14.0",

    "express-session": "latest",

    "request": "2.72.0"

  }


Please confirm if you use body-parser ?

Tushar -

you will need to confine your code to use an earlier version of request module (and others).

"engines": {"node": "0.10.32"},

From request v2.72.0 onward , the request module is based on node v4. That won't work on Apigee Edge with Trireme. Therefore you need

  "request": "2.71.0"

Check the package.json file for the node engine, for each module you use, to be sure.