JonAbrams / synth

The first back-end framework specially designed for single-page web applications
http://www.synthjs.com
MIT License
969 stars 70 forks source link

node request package using in synth #85

Closed Dave93 closed 9 years ago

Dave93 commented 9 years ago

Good evening Jon Abrams. I have one task. I have to make http request in back-end to the api of backendless.com with "request" package of nodejs. I do such action, because front-end user shouldn't see api keys of backendless from any js file. When i print recieved data to node console i see data, but when i return this data to front-end i don't see anything printing it to console. Could you help with this issue? Back-end code:

var request = require('request');
exports.getIndex = function(db) {
    request({
        uri: "https://api.backendless.com/v1/data/ib_companies?where=index_slider%3Dtrue", //o`
        method: "GET",
        headers: global.BackendlessApp
    }, function(error, response, body) {
        console.log(response);
        return response;

    });

};

Front-end code:

$http.get('/api/home' + $location.path()).then(function(res) {
    console.log(res);
});
JonAbrams commented 9 years ago

You'll need to use a promise friendly version of request, such as request-promise.

Your code would look something like:

var request = require('request-promise');
exports.getIndex = function(db) {
    return request({
        uri: "https://api.backendless.com/v1/data/ib_companies?where=index_slider%3Dtrue", //o`
        method: "GET",
        headers: global.BackendlessApp
    }).then(function (body) {
        return JSON.parse(body);
    });
};