I'm trying to request some information from an HTTPS API using Restify. The following works fine:
curl 'https://api.mercadolibre.com/sites/MLA/search?q=cartera&seller_id=156853713&limit=10&offset=0'
However, this hangs:
var client = restify.createClient({
url: "https://api.mercadolibre.com"
});
search = function(response, name, store_id, limit, offset) {
client.get("/sites/MLA/search?q=cartera&seller_id=156853713&limit=10&offset=0", function (err, req) {
assert.ifError(err); // connection error
var str = ''
req.on('result', function (err, v_res) {
assert.ifError(err); // HTTP status code >= 400
v_res.on('data', function (chunk) {
str += chunk;
});
v_res.on('end', function () {
response.send(str)
});
});
});
}
with the following error: ECONNRESET.
Googling around I found this post: Node.js Https request Error but the answer does not seem to work for me.
Any ideas?
Thanks
Related
I have a consistent problem with using the request module where, regardless of the URL I provide, I am getting a socket hang up error, generally a connection reset. What makes this more strange is that this code works on another developers machine without problem.
var request = require("request");
request("http://google.com", function(error, response, body) {
console.log(error);
console.log(response);
if (!error && response.statusCode === 200) {
}
});
This is a simplified version of the code but it illustrates the point. If I do something like this though
var http = require('http');
var options = {
host: 'www.google.com',
port: 80
};
http.get(options, function (resp) {
var bdy = "";
resp.on('data', function (chunk) {
bdy = bdy + chunk;
});
resp.on('end', function () {
//var r = JSON.parse(bdy);
console.log(bdy);
});
}).on("error", function (e) {
console.log("Got error: " + e.message);
});
I get a response back from Google as I would expect. What is strange is that both the request module and another module I am using (weather-js) exhibiting the same behavior: all requests result in some sort of socket error. Additionally, when I run Fiddler I can see the http.get request go out, but I never see an entry when the code from the request or weather-js module runs.
I am running Node 10.5.0 on Windows 10.
I am trying to fetch a page from port 1717 but when my bandwidth is unavailable, the http.get on error callback logs errno ENOENT whereas when I turn my bandwidth back on, it logs errno ECONNRESET. Regardless of my computer being offline or not, the url http://localhost:1717/admin/available/ ALWAYS returns content in the browser as long as the server is up and running. I've tried using postman and I have also tried using request method of the http module instead of get. I ended request after using it but I still got the same errors.
Meanwhile I have tried getting other links besides those on localhost and it fetched them. In some other threads, I saw people suggest I use hostname 127.0.0.1 instead of localhost. That too did not work so I switched to the request module from npm then there was a slight difference in behavior. In the server.js, I have sommething like this for GET requests hitting /admin/available/
console.log('giving you table', table)
res.writeHead(200, {"Content-Type": "text/html"});
res.end(table);
However, when I use the request module, it throws the error ECONNRESET but in the CLI window where the server is running, this console.log('giving you table', table) is logged, meaning the server does see that request but somehow, the module still throws ECONNRESET and the body and response variables are undefined, claiming it cannot see it. What can I do about this?
I'll be posting my code below in case I'm missing something.
var http = require('http'),
component = require('../lib/render-component'),
render = {username: '', available: '', orders: '', frequent: ''};
// for simplicity
var request = require('request');
request('http://localhost:1717/admin/available', function(err, res, body) {
console.log(err, res, body)
});
// intended use scenario
http.get({port: 1717, path: '/admin/available/', headers: {Accept: 'text/html'}}, function(res) {
var temp = '';
res.setEncoding('utf8');
console.log('inside get');
res.on('data', function (chunk) {
temp += chunk;
}).on('end', function() {
render.available = temp;
http.get('http://localhost:1717/admin/order/?page=0', function(res) {
var temp = ''
res.setEncoding('utf8');
res.on('data', function (chunk) {
temp += chunk;
}).on('end', function() {
render.orders = temp;
ordersModel.find({status: 'delivered'}, 'food', function (err, orders) {
if (err) throw err;
var hashMap = [], returnArr = [];
orders.forEach(function (order) {
hashMap.push(order.toObject()['food'].split(","));
})
hashMap.reduce(function(a, b) {
return a.concat(b)
}, []).forEach(function(item) {
if ((k = returnArr.findIndex(function(elem) {
return elem[0] == item;
})) != -1) {
returnArr[k][1]++;
}
else returnArr.push([item, 1]);
})
// filter the ones with the highest value
hashMap = [], returnArr = returnArr.sort(function(a, b) {
return b[1] - a[1];
}).slice(0, 5).forEach(function(elem) {
hashMap.push({name: elem[0], counter: elem[1]})
});
render.frequent = component("frequent", hashMap);
console.log(render)
}) // orders model find
}); // get orders
}).on('error', function(e) {
console.log(e)
});
}); // available on end
}).on('error', function(e) {
console.log('err available', e)
}); // available on error
Please help me. I've been stuck for three days now.
I have the following backend http endpoint implemented with NodeJS and expressJS
app.get("/api/stocks/lookup/:qry", function(req, res) {
getJson(lookupSearch(req.params.qry), function(json) {
var quotes = [];
und.forEach(json, function(d) {
getJson(quoteSearch(d.Symbol), function(j) {
quotes.push(j);
if (json.length == quotes.length) {
res.send(quotes);
}
});
});
});
});
var getJson = function(search, cb) {
http.request(search, function(response) {
var raw = '';
response.on('data', function(d) {
raw += d;
});
response.on('end', function() {
cb(JSON.parse(raw));
});
response.on('error', function(err) {
console.error(err);
});
}).end();
};
What I do not understand is why this only works sometimes and why other times I get this error message from curl: curl: (52) Empty reply from server My solution that checks the lengths of json and quotes seems correct to me and so I think I must have misused some library. However, as I am new to asynchronous code (the http request in getJson) I am not 100% sure this is correct.
I asked a question with similar code here: syntax and methods for placing callbacks in nodejs Note that the 'homebrewed' solution with incrementing and decrementing a count only works sometimes as well. I do not want to use JQuery inside node either.
Why does the above http request only work sometimes?
I need to connect to a web page and return the status code of the page, which I've been able to achieve using http.request however the pages I need to request can take a long time, sometimes several minutes, so I'm always getting a socket hang up error.
I'm using the following code so far:
var reqPage = function(urlString, cb) {
// Resolve the URL
var path = url.parse(urlString);
var req = http.request({
host: path.hostname,
path: path.pathname,
port: 80,
method: 'GET'
});
req.on('end', function() {
cb.call(this, res);
});
req.on('error', function(e) {
winston.error(e.message);
});
};
What do I need to do to ensure that my application still attempts to connect to the page even if it's going to take a few minutes?
Use the request module and set the timeout option to an appropriate value (in milliseconds)
var request = require('request')
var url = 'http://www.google.com' // input your url here
// use a timeout value of 10 seconds
var timeoutInMilliseconds = 10*1000
var opts = {
url: url,
timeout: timeoutInMilliseconds
}
request(opts, function (err, res, body) {
if (err) {
console.dir(err)
return
}
var statusCode = res.statusCode
console.log('status code: ' + statusCode)
})
Add this if you don't want to use a higher level http client like request or superagent , then add this...
req.on("connection", function(socket){
socket.setTimeout((1000*60*5)); //5 mins
});
Here is the thing :
I have a client which sends data to a server. This server has to contact an external A.P.I. and send back its response to the client. I just can't figure out how and where I can contact the external A.P.I once the server has got the client data.
I route client data like this :
app.post('/getAutoComplete', routes.read);
routes.read retrieves the data within req.body. With my nodejs version (without express framework), I then request the api this way :
var http = require('http'), options = {
host : "192.168.1.38",
port : 8080,
path : "/myURL",
method : 'POST'
};
var webservice_data = "";
var webservice_request = http.request(options, function(webservice_response)
{
webservice_response.on('error', function(e){ console.log(e.message); });
webservice_response.on('data', function(chunk){ webservice_data += chunk;});
webservice_response.on('end', function(){res.send(webservice_data);});
});
webservice_request.write(req.body);
webservice_request.end();
The problem is that i'd like to use native expressJS method like app.post but I don't know how because :
Express (app) object is not available here (declared in app.js but not in the route file)
I don't know how to send POST data with app.post
Any suggestion ?
app.post('/getAutoComplete', routes.read);
// assuming routes.read lookes something like this
routes.read = function read(req, res) {
var http = require('http'), options = {
host : "192.168.1.38",
port : 8080,
path : "/myURL",
method : 'POST'
};
var webservice_data = "";
var webservice_request = http.request(options, function(webservice_response)
{
webservice_response.on('error', function(e){ console.log(e.message); });
webservice_response.on('data', function(chunk){ webservice_data += chunk;});
webservice_response.on('end', function(){res.send(webservice_data);});
});
webservice_request.write(req.body);
webservice_request.end();
};
Also check out https://github.com/mikeal/request It's the de-facto module for doing web requests in node.
routes.read is a function. You can call it with extra parameters, so for example
app.post('/getAutoComplete', function(req,res) {
var q = req.query.q; // or whatever data you need
routes.read(q, function(err, response) {
if (err) throw err;
return res.json(response);
});
});
Now make the routes.read function use the first parameter as the query and when it's gathered the response from the remote API, call the second parameter with any error as the first parameter and the response as the second one.
Update This answer has already been picked as an answer, but it'd be more helpful if I showed an example of routes.read, too:
routes.read = function(q, cb) {
// pretend we calculate the result
var result = q * 10;
if (result > 100) {
// call the callback with error set
return cb("q value too high");
}
// all is well, use setTimeout to demonstrate
// an asynchronous return
setTimeout(function() { cb(null, result) }, 2000);
};