NodeJS passing results form HTTPS request to the view - node.js

I am making an HTTPS request to an API (wrike.com) and everythings working except I'm struggling to pass the result to my view in order to display in the frontend.
This is the function code:
module.exports.getWrikeFolder = function(user, callback) {
User.findOne({email: user}, function(err, doc){
if(err) throw err;
var accessToken = doc.wrikeaccess;
console.log(accessToken);
var data = querystring.stringify({
"descendants": true,
"project": false
});
var options = {
host: 'www.wrike.com',
method: 'GET',
path: '/api/v3/folders/',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'bearer ' + accessToken
}
}
var folders = [];
var request = https.request(options, function(res) {
res.on('data', function (chunk) {
folders.push(chunk);
}).on('end', function() {
folders = Buffer.concat(folders).toString();
console.log(folders);
});
});
request.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
request.write(data);
request.end(err, folders);
});
}
And this is the code for the route:
router.get('/dmd', ensureAuthenticated, function(req, res, next) {
wrike.getWrikeFolder(user, function(err, folders) {
//if(err) throw err;
res.render('dmd-codes', {
title: 'DMD Codes',
nav: 'Admin',
folders: folders
});
});
});
I know the function works as the console.log(folders); line shows the expected result in the console. What am I missing to get the result into the route.
Thanks for any help.

Looks like you never execute your callback in the module that does the http request to the API, which means the res.render() wouldn't execute. Try adding the execution of callback inside of getWrikeFolder():
module.exports.getWrikeFolder = function(user, callback) {
User.findOne({email: user}, function(err, doc){
// if(err) throw err; // NOOOO! Use callbacks ;)
if (err) return callback(err);
// ... (your code)
var request = https.request(options, function(res) {
res.on('data', function (chunk) {
folders.push(chunk);
}).on('end', function() {
folders = Buffer.concat(folders).toString();
console.log(folders);
// make sure to execute callback!
callback(null, folders);
});
});
request.on('error', function(e) {
console.log('problem with request: ' + e.message);
// don't forget to use the callback!!
return callback( new Error('problem with request: ' + e.message) );
});
request.write(data);
request.end(err, folders);
});
}

Related

how to get the data from http.get

I have created a NodeJS server and created promises for the HTTP.get method and calling the get method function in created server but it showing error options.uri
var http = require('http');
var request = require('request');
var rese = null;
function initialize() {
var options = {
host: 'httpbin.org',
path: '/json',
};
return new Promise(function (resolve, reject) {
request.get(options, function (err, res, body) {
if (err) {
reject(err);
} else {
resolve(JOSN.parse(body));
}
/** res.on("data", function(chunk) {
//console.log("BODY: " + chunk);
result=chunk;
});*/
})
})
}
http.createServer(function (req, res) {
if (req.url == '/my') {
/**result=res.on("data", function(chunk) {
console.log("BODY: " + chunk);
});*/
var initializePromise = initialize();
initializePromise.then(function (res) {
rese = result;
console.log("Initialized user details");
// Use user details from here
console.log(userDetails)
}, function (err) {
console.log(err);
});
//res.end(result);
}
else {
res.end('please find the correct path');
}
}).listen(2000);
error:options.uri is a required argument
Your get method should include uri, The request api get call structure is as follows,
request.get("http://bin.org/json", options, function (err, res, body) {
if (err) {
reject(err);
} else {
resolve(JSON.parse(body));
}
/** res.on("data", function(chunk) {
//console.log("BODY: " + chunk);
result=chunk;
});*/
})
Try to make this changes, it will work

var function calling for unknown reason

I have the code below, and it seems to call the var promiseFeedback is called and I don't know why... This means it is called even when an error occurs when I create document. Whereas is should only be called if there is no err in the createDocument.
Is anyone able to clear up why?
if (json) {
createDocument(documentUrl, context, json, function(res){
var promiseFeedback = callFB (context, res);
var collection = `mydb`
client.createDocument(collection, res, (err, result) => {
if(err) {
context.log(err);
return context.done();
} else {
Promise.all([promiseFeedback]).then(function(results){
context.log("promiseFeedback: " + results[0]);
context.done();
});
}
});
});
}
function callFB(context, res) {
return new Promise((resolve, reject) => {
var requestUrl = url.parse( URL );
var requestBody = {
"id": res.id
};
var body = JSON.stringify( requestBody );
const requestOptions = {
hostname: requestUrl.hostname,
path: requestUrl.path,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(body),
}
};
var request = https.request(requestOptions, function(res) {
var data ="";
res.on('data', function (chunk) {
data += chunk
});
res.on('end', function () {
resolve(true);
})
}).on('error', function(error) {
context.log("request error:", error);
resolve(false);
});
request.write(body);
request.end();
});
}
var promiseFeedback = callFB (context, res);
This statement executes callFB immediately, not just assigns another name to the promise. This promise callFB is out of the callback(scope) of err and Promise.all([promiseFeedback]), it runs no matter what the result of client.createDocument is.
To fix this:
Remove var promiseFeedback = callFB (context, res); and change Promise.all([promiseFeedback]) to callFB(context, res). You don't need to use Promise.all as you only have one promise to resolve.
Or you can just move var promiseFeedback = callFB (context, res); into else segment.

How do I wait for a json to load before using it on node.js? [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 5 years ago.
[EDIT]
I figured it out. The code ends up like this:
//getTrelloJSON.js
var request = require('request');
'use strict';
function getProjJSON(requestURL, callback){
request.get({
url: requestURL,
json: true,
headers: {'User-Agent': 'request'}
}, (err, res, data) => {
if (err) {
console.log('Error:', err);
} else if (res.statusCode !== 200) {
console.log('Status:', res.statusCode);
} else {
callback(data);
}
});
}
module.exports.getProjJSON = getProjJSON;
And
//showData.js
var getJSON = require('./getTrelloJSON');
getJSON.getProjJSON('https://trello.com/b/saDpzgbw/ld40-gem-sorceress.json', (result) => {
var lists = result.lists;
console.log(lists);
});
I run node showData.js and it gets the json and then I can manipulate it as needed. I printed just to show it works.
[EDIT END]
I'm new to node.js and I am facing a noob problem.
This code is supposed to request a JSON from a public trello board and return an object with a section of trello's json (lists section).
The first console.log() does not work but the second does.
How do I make it wait for the completion of getProjJSON() before printing it?
var request = require('request');
'use strict';
//it fails
console.log(getProjJSON('https://trello.com/b/saDpzgbw/ld40-gem-sorceress.json'));
function getProjJSON(requestURL){
request.get({
url: requestURL,
json: true,
headers: {'User-Agent': 'request'}
}, (err, res, data) => {
if (err) {
console.log('Error:', err);
} else if (res.statusCode !== 200) {
console.log('Status:', res.statusCode);
} else {
//it works
console.log(data.lists);
return data.lists;
}
});
}
Node.js is all about callbacks.
And here you just not register the callbacks for data.
var client = require('http');
var options = {
hostname: 'host.tld',
path: '/{uri}',
method: 'GET', //POST,PUT,DELETE etc
port: 80,
headers: {} //
};
//handle request;
pRequest = client.request(options, function(response){
console.log("Code: "+response.statusCode+ "\n Headers: "+response.headers);
response.on('data', function (chunk) {
console.log(chunk);
});
response.on('end',function(){
console.log("\nResponse ended\n");
});
response.on('error', function(err){
console.log("Error Occurred: "+err.message);
});
});
or here is a full example, hope this solve your problem
const postData = querystring.stringify({
'msg' : 'Hello World!'
});
const options = {
hostname: 'www.google.com',
port: 80,
path: '/upload',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(postData)
}
};
const req = http.request(options, (res) => {
console.log(`res_code: ${res.statusCode}`);
console.log(`res_header: ${JSON.stringify(res.headers)}`);
res.setEncoding('utf8');
res.on('data', (chunk) => {
console.log(`res_data: ${chunk}`);
});
res.on('end', () => {
console.log('end of response');
});
});
req.on('error', (e) => {
console.error(`response error ${e.message}`);
});
//write back
req.write(postData);
req.end();

Using Q library for HTTP api response testing in nodejs

how to use Q to make it wait until previous response has come from the server.
What I am looking to do here is compare the response from test server and production server for the same request.
I get the responses back from both the servers, but unable to compare them since the assert statement is executed before the response comes back.
Any one know what I am doing wrong. heres the code.
var Q = require('q');
var path='';
var prodResponse = '';
var tstReponse = '';
Q.fcall(readFile())
.then(secondFunction())
.then(thirdFunction())
.then(function(){
console.log("prodResponse: "+prodResponse);
console.log("tstResponse: "+tstResponse);
assert.strictEqual(prodResponse, tstResponse)
})
.catch(function(){
console.log('error occurred');
})
.done();
function readFile(){
fs.readFile('hostname.json', function (err, data) {
if (err) return console.error(err);
path = JSON.parse(data);
return JSON.parse(data);
});
}
function secondFunction(){
var prodOptions = {
hostname: 'somehostname.com',
port: 80,
path: "/path?"+path.path,
method: 'POST',
headers: {
'Content-Type': 'application/json;charset=UTF-8'
},
auth : ''
};
return http.request(prodOptions, function(res) {
console.log('Prod');
res.setEncoding('utf8');
res.on('data', function (chunk) {
prodResponse = chunk;
return chunk;
});
res.on('end', function() {
console.log('No more data in response.');
})
}).on('error', function(e) {
console.log('problem with request: ' + e.message);
}).end();
}
function thirdFunction(){
// same a second, only difference is the response http.
}
There is multiple errors in your code
Q.fcall(readFile())
Your q variable is q and not Q. So this line will crash because Q is undefined (javascript is case sensitive).
Then, readFile doesn't return any promise (in fact, it returns nothing). So the q library can't use anything to wait the end of any asynchronous work. The then callbacks will be fired immediatly.
You can use Q.ninvoke to make your readFile function return a promise, and you can use Q.defer to create and return a promise from your secondFunction:
var Q = require('q');
var path='';
var prodResponse = [];
var tstReponse = '';
readFile()
.then(secondFunction())
.then(thirdFunction())
.then(function(){
console.log("prodResponse: "+prodResponse);
console.log("tstResponse: "+tstResponse);
assert.strictEqual(prodResponse, tstResponse)
})
.catch(function(){
console.log('error occurred');
})
.done();
function readFile(){
return Q.ninvoke(fs, 'readFile', 'hostname.json').then(function (data) {
path = JSON.parse(data);
return path;
}, function (err) {
console.error(err);
});
}
function secondFunction(){
var prodOptions = {
hostname: 'somehostname.com',
port: 80,
path: "/path?"+path.path,
method: 'POST',
headers: {
'Content-Type': 'application/json;charset=UTF-8'
},
auth : ''
};
var defer = Q.defer();
var chunks = [];
http.request(prodOptions, function(res) {
console.log('Prod');
res.setEncoding('utf8');
res.on('data', function (chunk) {
chunks.push(chunk);
});
res.on('end', function() {
console.log('No more data in response.');
prodResponse = chunks.join('');
defer.resolve(prodResponse);
})
}).on('error', function(e) {
console.log('problem with request: ' + e.message);
defer.reject(e);
}).end();
return defer.promise;
}
function thirdFunction(){
// same a second, only difference is the response http.
}

how to do Auth in node.js client

I want to get use this rest api with authentication. I'm trying including header but not getting any response. it is throwing an output which it generally throw when there is no authentication. can anyone suggest me some solutions. below is my code
var http = require('http');
var optionsget = {
host : 'localhost', // here only the domain name
port : 1234,
path:'/api/rest/xyz',
headers: {
'Authorization': 'Basic ' + new Buffer('abc'+ ':' + '1234').toString('base64')
} ,
method : 'GET' // do GET
};
console.info('Options prepared:');
console.info(optionsget);
console.info('Do the GET call');
var reqGet = http.request(optionsget, function(res) {
console.log("statusCode: ", res.statusCode);
res.on('data', function(d) {
console.info('GET result:\n');
process.stdout.write(d);
console.info('\n\nCall completed');
});
});
reqGet.end();
reqGet.on('error', function(e) {
console.error(e);
});
The request module will make your life easier. It now includes a Basic Auth as an option so you don't have build the Header yourself.
var request = require('request')
var username = 'fooUsername'
var password = 'fooPassword'
var options = {
url: 'http://localhost:1234/api/res/xyz',
auth: {
user: username,
password: password
}
}
request(options, function (err, res, body) {
if (err) {
console.dir(err)
return
}
console.dir('headers', res.headers)
console.dir('status code', res.statusCode)
console.dir(body)
})
To install request execute npm install -S request
In your comment you ask, "Is there any way that the JSOn I'm getting in the command prompt will come in the UI either by javascript or by Jquery or by any means."
Hey, just return the body to your client:
exports.requestExample = function(req,res){
request(options, function (err, resp, body) {
if (err) {
console.dir(err)
return;
}
// parse method is optional
return res.send(200, JSON.parse(body));
});
};

Resources