I can't reach my value until my function is ending.. I tried callbacks but it seems to doesn't work..
exports.helloHttp = function helloHttp (req, res) {
var url = "https://api.coindesk.com/v1/bpi/currentprice.json";
var btcValue
require('https').get(url, function(res, btcValue){
var body = '';
res.on('data', function(chunk){
body += chunk;
});
res.on('end', function(){
btcValue = JSON.parse(body);
callback(btcValue);
});
}).on('error', function(e){
console.log("Got an error: ", e);
});
console.log("Got a respons ", btcValue);
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify({ "speech": response, "displayText": response
}));
};
Thanks a lot in advance
I've written a standalone example based on your code:
var http = require('http'),
https = require('https');
http.createServer(function(req, res) {
// You can largely ignore the code above this line, it's
// effectively the same as yours but changed to a standalone
// example. The important thing is we're in a function with
// arguments called req and res.
var url = 'https://api.coindesk.com/v1/bpi/currentprice.json';
var request = https.get(url, function(response) {
var body = '';
response.on('data', function(chunk) {
body += chunk;
});
response.on('end', function() {
// TODO: handle JSON parsing errors
var btcValue = JSON.parse(body);
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({
btcValue: btcValue
}));
});
});
request.on('error', function(e) {
console.error(e);
});
// Runs this example on port 8000
}).listen(8000);
The most important change is moving the code for handling our response (res) into the 'end' listener of the coindesk response. That call to coindesk is asynchronous so we have to wait for the 'end' event before we try to act on it.
You reference a variable called response twice when building your JSON. Your code didn't define response but I've assumed that it is supposed to be related to the btcValue coming back from the call to coindesk. I wasn't sure exactly what you wanted there so I've just wrapped btcValue in another object for demonstration purposes.
In your original code you had this line:
require('https').get(url, function(res, btcValue){
That second argument, which you've called btcValue, doesn't exist so it will just be set to undefined.
I've changed send to end but that isn't a significant change. I assume you're using Express (which provides a send method) whereas my example is not.
Related
Am trying to design a REST api which will throw an aggregated response from multiple apis.
Following is the NodeJS code am trying to execute -
Pseudo Code start
//endpoint to be called from a browser / REST client
router.get('/api/v1/getItems', (req, response, next) => {
var result = {} // hold the aggregated response from multiple apis
//internally fire another endpoint & add the response over to the var result
http.get(endpoint 1, function(resp){
add response to result})
http.get(endpoint 2, function(resp){
add response to result
})
return response.json(result);
}
Pseudo Code end
// endpoint to be called from the browser or REST Client.
router.get('/api/v1/getItems', (req, response, next) => {
var results = {};
// Nested Endpoint 1
var optionsgetmsg = {
host : 'host.domain.com', // tthe domain name
port : 9043,
path : '/services/itemdata', // the rest of the url
method : 'GET' // do GET
};
//child endpoint
var reqGet = http.request(optionsgetmsg, function(res) {
res.on('data', function(d) {
console.log("d "+ d); // child response
results.itemdata = d;
return response.send(results);
//process.stdout.write(d);
});
res.on('end', function(d){
})
});
reqGet.end();
reqGet.on('error', function(e) {
console.error(e);
});
});
The result in the above case should be the output 'd'. The output 'd' is the response from the child endpoint.
Actual result am getting is an empty object. {}
If you are sending JSON, you must set the headers correctly and the response:
//child endpoint
var reqGet = http.request(optionsgetmsg, function(res) {
res.on('data', function(d) {
res.setHeader('Content-Type', 'application/json');
var results = d;
response.send(JSON.stringify(results));
});
It is unclear as to what exactly you are asking for.
I'm trying to read a PDF from a URL and display it to a user's browser (via the passed in 'response' object). I've tried to use the code below and it works sometimes, but generally fails:
function writePdfToBrowser(url, response) {
http.get(url, function(res) {
logger.verbose('about to start download...');
var chunks = [];
res.on('data', function(chunk) {
chunks.push(chunk);
});
res.on("end", function() {
logger.verbose('downloaded');
var buffer = new Buffer.concat(chunks);
//write downloaded pdf to the original response
response.write(buffer);
//response.send(buffer);
response.end();
});
}).on("error", function() {
logger.error("error!");
});
}
In the new page where I attempted to load the pdf it would just say "Failed to load pdf".
I'm new to Node, so not sure where the problem lies, any ideas? Anyone have any working code to do the same thing?
Thank you for any help!
Mark
Use piping:
function pipe(url, res) {
var request = http.get(url, function(response) {
res.writeHead(response.statusCode, response.headers)
response.pipe(res);
});
request.on('error', function(error){
res.statusCode = 500;
res.end(error.message);
});
}
... and please provide next time more information about what and how it fails, some logs, inspect response im browser before. And so on..
in my project I have to do a request to upcDatabase.com, I amworking with nodeJS, I get the answer from the server but I do not how to extractthe data this are the important part of my code:
module.exports = function (http,upc){
var upc_ApiKey = "XXX",
url = "http://upcdatabase.org/api/json/"+upc_ApiKey+'/'+upc;
http.get(url,function(resp){
// my code to read the response
I do not get any error, but the resp is a big Json and I do not know where to find the data
I would recommend you using the superagent module. It provides much more functionality than the built-in http request and it will automatically parse the response for you.
request
.get(url)
.end(function(err, res) {
if (res.ok) {
// Her ethe res object will be already parsed. For example if
// the server returns Content-Type: application/json
// res will be a javascript object that you can query for the properties
console.log(res);
} else {
// oops, some error occurred with the request
// you can check the err parameter or the res.text
}
});
You could achieve the same with the built-in http module but with much more code:
var opts = url.parse(url);
opts.method = "GET";
var req = http.request(opts, function (res) {
var result = "";
res.setEncoding("utf8");
res.on("data", function (data) {
result += data;
});
if (res.statusCode === 200) {
res.on("end", function () {
// Here you could use the result object
// If it is a JSON object you might need to JSON.parse the string
// in order to get an easy to use js object
});
} else {
// The server didn't return 200 status code
}
});
req.on("error", function (err) {
// Some serious error occurred during the request
});
// This will send the actual request
req.end();
I wish to create a first node js app. For this I need to pull the public twitter tweet and output tweet as a request response. I searched a lot but I got code that written in older node version code(ie createServer, addListener something like this). How we write code for request a twitter call and output the json as response in node version 0.6.18?
Following is the older code I tried
var http = require("http");
var events = require("events");
var port = 8001;
var twitterClient = http.createClient(80, 'api.twitter.com');
var tweetEmitter = new events.EventEmitter();
var request = twitterClient.request("GET", "/1/statuses/public_timeline.json", {"host": "api.twitter.com"});
function getTweats() {
request.addListener("response", function (response) {
var body = "";
response.addListener("data", function (data) {
body += data;
});
response.addListener("end", function (end) {
var tweets = JSON.parse(body);
if (tweets.length > 0) {
tweetEmitter.emit("tweets", tweets);
console.log(tweets, 'tweets loaded');
}
});
});
request.end();
}
setInterval(getTweats(), 1000);
http.createServer(function (request, response) {
var uri = url.parse(request.url).pathname;
console.log(uri);
if (uri === '/stream') {
var cb = function (tweets) {
console.log('tweet'); // never happens!
response.writeHead(200, {"Content-Type": "text/plain"});
response.write(JSON.stringify(tweets));
response.end();
clearTimeout(timeout);
};
tweetEmitter.addListener("tweets", cb);
// timeout to kill requests that take longer than 10 secs
var timeout = setTimeout(function () {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write(JSON.stringify([]));
response.end();
tweetEmitter.removeListener("tweets", cb);
}, 10000);
} else {
loadStaticFile(uri, response);
}
}).listen(port);
console.log("Server running at http://localhost:" + port + "/");
And got error
$ node tweet.js
Server running at http://localhost:8001/
timers.js:223
callback.apply(timer, args);
^
TypeError: Cannot call method 'apply' of undefined
at Timer.ontimeout (timers.js:223:14)
Your error is a common one. Instead of:
setInterval(getTweats(), 1000);
you need
setInterval(getTweats, 1000);
This is because you want setInterval to call the getTweats function itself.
Your code has getTweats() with parentheses instead. This calls getTweats first, and gives the result to setInterval. Since this result is undefined (your function doesn't return anything), node cannot call it.
app.get('/', function(req, res){
var options = {
host: 'www.google.com'
};
http.get(options, function(http_res) {
http_res.on('data', function (chunk) {
res.send('BODY: ' + chunk);
});
res.end("");
});
});
I am trying to download google.com homepage, and reprint it, but I get an "Can't use mutable header APIs after sent." error
Anyone know why? or how to make http call?
Check out the example here on the node.js doc.
The method http.get is a convenience method, it handles a lot of basic stuff for a GET request, which usually has no body to it. Below is a sample of how to make a simple HTTP GET request.
var http = require("http");
var options = {
host: 'www.google.com'
};
http.get(options, function (http_res) {
// initialize the container for our data
var data = "";
// this event fires many times, each time collecting another piece of the response
http_res.on("data", function (chunk) {
// append this chunk to our growing `data` var
data += chunk;
});
// this event fires *one* time, after all the `data` events/chunks have been gathered
http_res.on("end", function () {
// you can use res.send instead of console.log to output via express
console.log(data);
});
});