Why Does My NodeJS Code Throw: ECONNRESET error? - javascript

My program parses a text document with HTTPS proxies in a string array. It then makes a GET request to ipify.org.
However, my program is throwing: Error: read ECONNRESET at TCP.onStreamRead (internal/stream_base_commons.js:183:27). It terminates my program instead of throwing an error and continuing.
My code:
var fs = require('fs');
var async = require("async");
var request = require("request");
var proxies = fs.readFileSync(__dirname + '\\proxies.txt').toString().split("\n");
console.log(proxies.length + ' proxies loaded!');
async.forEachOf(proxies, proxy => {
request({
'url':'https://api.ipify.org',
'method': "GET",
'proxy': 'http://' + proxy
},function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
else {
console.log(error.message);
}
})
}, err => {
console.log(err.message);
});
Any help is highly appreciated :)

[Node js ECONNRESET
the error is explained here.I think the url is not being connected for some reason...

Related

Failed to load resource: net::ERR_CONNECTION_REFUSED when fetching from localhost

I'm trying to make a request to my localhost server but am getting the error Failed to load resource: net::ERR_CONNECTION_REFUSED.
Here is my front end code:
(async () => {
const data = await fetch('http://localhost:8080/articles/', {
headers: { "Access-Control-Allow-Origin": "http://localhost:8080/articles/" }
});
const articles = await data.json()
console.log(articles)
})();
and backend code:
app.get("/articles/", function (req, res) {
let inputValue = req.body.page;
let pages = Math.ceil(totalResults / 10)
page = scripts.iteratePages(inputValue, page, pages);
request("https://newsapi.org/v2/top-headlines?q=" + initialQ +
"&category=sports&pageSize=10&page=" + page + "&sortBy=relevance&apiKey=" +
apiKey, function (error, response, body) {
if (!error && response.statusCode == 200) {
let data = JSON.parse(body);
let articles = scripts.articlesArr(data);
res.json({ articles: articles });
} else {
res.redirect("/");
console.log(response.body);
}
});
});
I've done some research myself, which points to using my private IP address instead of localhost and am getting an ... is not a function error in the console:
let ipAddress = "blah.blah.blah.blah"
(async () => {
const data = await fetch('http://' + ipAddress + ':8080/articles/', {
headers: { "Access-Control-Allow-Origin": "http://" + ipAddress + ":8080/articles/" }
});
const articles = await data.json()
console.log(articles)
})();
Any help would be great.
I think the error was caused by cross domain issues. You make a misunderstanding about CORS headers. The headers are not used by request but response.
If javascript program is making a cross domain request, the browser will first send an OPTION request with same parameters to the server. If backend on the server consider the request is legal, a response with only CORS headers will be send back to the browser, and then the browser check the CORS header against current environment. If CORS headers are suitable, the browser will finaly send the real request to server, or throw an error otherwise.
https://en.wikipedia.org/wiki/Cross-origin_resource_sharing

Python to Node/JS

I'm reading a documentation for api but it's example is in python.
Here's the code:
import http.client
conn = http.client.HTTPSConnection("api.sportradar.us")
conn.request("GET", "/mma-t1/schedule.xml?api_key={your_api_key}")
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
But I'm using node. I tried to convert above code to this:
import request from 'request';
const key = Meteor.settings.sportradar.mma_v1;
const conn = `api.sportradar.us/mma-t1/schedule.xml?api_key=${key}`;
request(conn, (err, res, body) => {
console.log('error:', err);
console.log('statusCode:', res && res.statusCode);
console.log('body:', body);
});
but I'm getting an error: Invalid URI.
The response will be in XML if that matters.
Can someone help please?
In node you usually need to append http or https to the uri.

TypeError: Cannot read property 'sid' of undefined

I'm trying to send off an sms (which works in Twilio's API Explorer) but seems to fail under my node installation. I've just done a complete uninstall and reinstalled with no avail.
Error
7 Oct 21:28:37 - [nodemon] starting `node scraper.js`
Free on Xbox One, Xbox 360, PS3, PS4: Tales from the Borderlands (Episode 1)
/Users/rhysedwards/Downloads/insightful/ozbargain/node_modules/twilio/node_modules/q/q.js:126
throw e;
^
TypeError: Cannot read property 'sid' of undefined
at /Users/rhysedwards/Downloads/insightful/ozbargain/scraper.js:39:31
at /Users/rhysedwards/Downloads/insightful/ozbargain/node_modules/twilio/node_modules/q/q.js:1924:17
at flush (/Users/rhysedwards/Downloads/insightful/ozbargain/node_modules/twilio/node_modules/q/q.js:108:17)
at doNTCallback0 (node.js:408:9)
at process._tickCallback (node.js:337:13)
7 Oct 21:28:39 - [nodemon] app crashed - waiting for file changes before starting...
Error with stripped back twilio code;
7 Oct 22:24:44 - [nodemon] starting `node scraper.js`
/Users/rhysedwards/Downloads/insightful/ozbargain/node_modules/twilio/node_modules/q/q.js:126
throw e;
^
TypeError: Cannot read property 'sid' of undefined
at /Users/rhysedwards/Downloads/insightful/ozbargain/scraper.js:12:24
at /Users/rhysedwards/Downloads/insightful/ozbargain/node_modules/twilio/node_modules/q/q.js:1924:17
at flush (/Users/rhysedwards/Downloads/insightful/ozbargain/node_modules/twilio/node_modules/q/q.js:108:17)
at doNTCallback0 (node.js:408:9)
at process._tickCallback (node.js:337:13)
7 Oct 22:24:46 - [nodemon] app crashed - waiting for file changes before starting...
Code
var accountSid = 'AC*******';
var authToken = 'da********';
var fs = require('fs'),
request = require('request'),
cheerio = require('cheerio'),
client = require('twilio')(accountSid, authToken);
url = 'http://www.ozbargain.com.au';
request(url, function(error, response, html) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(html);
var $el = $("a:contains('Xbox')");
if ($el.length) {
client.messages.create({
to: "61448141065",
from: "+61418739508",
body: "hey",
}, function(err, message) {
console.log(message.sid);
});
console.log($el.text());
} else {
console.log('hey');
}
}
});
The callback of client.messages.create is used as
}, function(err, message) {
console.log(message.sid);
});
When there will be error, the first parameter of the callback err will contain the information related to the error, and the second parameter message will be undefined.
Update the code as follow to handle the erroneous conditions
}, function (err, message) {
if (err) {
// Handle error
// Show appropriate message to user
} else {
// No error
if (message.sid) {
// Use sid here
}
}
console.log(message.sid);
});
You may simply change console.log(message.sid);
to
if(err) {
console.log(err.message);
}
Twilio developer evangelist here.
There are a couple of things wrong on your code on the twilio side of things.
client.messages.create({
to: "61448141065",
from: "+61418739508",
body: "hey",
}, function (err, message) {
console.log(message.sid);
});
on the Toyou're missing a + before the phone number, and after the body, you have an extra comma which you need to remove.
Your final code should look like the following:
var accountSid = 'AC*******';
var authToken = 'da********';
var fs = require('fs'),
request = require('request'),
cheerio = require('cheerio'),
client = require('twilio')(accountSid, authToken);
url = 'http://www.ozbargain.com.au';
request(url, function(error, response, html) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(html);
var $el = $("a:contains('Xbox')");
if ($el.length) {
client.messages.create({
to: "+61448141065",
from: "+61418739508",
body: "hey"
}, function(err, message) {
console.log(message.sid);
});
console.log($el.text());
} else {
console.log('hey');
}
}
});
I tested it here and it worked fine after changing that.
Hope this helps you.
UPDATE:
Try changing your code to just this:
var accountSid = 'AC*******';
var authToken = 'da********';
var fs = require('fs'),
client = require('twilio')(accountSid, authToken);
client.messages.create({
to: "+61448141065",
from: "+61418739508",
body: "hey"
}, function (err, message) {
console.log(message.sid);
});
It's because your call failed and there is no message object. Replace
console.log(message.sid);
with
if (err) console.log(err.message);
if (message) console.log(message.sid);
Then you will see the error message.

Node script to import reddit JSON won't work

I want to import the reddit json thread into my mongodb local server, so I can begin analyzing it. Please help me out.
app.js file below
var MongoClient = require('mongodb').MongoClient
, request = require('request');
MongoClient.connect('mongodb://localhost:27017/trasmatter', function(err, db) {
if(err) throw err;
request('https://www.reddit.com/r/Seattle/comments/3e0q5u/why_metro_matters_infographic/.json', function (error, response, body) {
if (!error && response.statusCode == 200) {
var obj = JSON.parse(body);
var stories = obj.data.children.map(function (story) { return story.data; });
db.collection('reddit').insert(stories, function (err, data) {
if(err) throw err;
console.dir(data);
db.close();
});
}
});
});
Console error after running 'node app.js'
app.js:11
var stories = obj.data.children.map(function (story) { return sto
^
TypeError: Cannot read property 'children' of undefined
at Request._callback (/Users/yay/code/pulley/app.js:11:35)
at Request.self.callback (/Users/yay/code/pulley/node_modules/request/request.js:198:22)
at Request.emit (events.js:110:17)
at Request.<anonymous> (/Users/yay/code/pulley/node_modules/request/request.js:1057:14)
at Request.emit (events.js:129:20)
at IncomingMessage.<anonymous> (/Users/yay/code/pulley/node_modules/request/request.js:1003:12)
at IncomingMessage.emit (events.js:129:20)
at _stream_readable.js:908:16
at process._tickCallback (node.js:355:11)
Another script file that works successfully. (I just plugged in
another link and change localhost link)
var MongoClient = require('mongodb').MongoClient
, request = require('request');
MongoClient.connect('mongodb://localhost:27017/course', function(err, db) {
if(err) throw err;
request('http://www.reddit.com/r/technology/.json', function (error, response, body) {
if (!error && response.statusCode == 200) {
var obj = JSON.parse(body);
var stories = obj.data.children.map(function (story) { return story.data; });
db.collection('reddit').insert(stories, function (err, data) {
if(err) throw err;
console.dir(data);
db.close();
});
On your second example, when requesting http://www.reddit.com/r/technology/.json the JSON result is an object:
{"kind":"stuff","data":{}}
But the first request result to an array of objects :
[{"kind":"stuff", "data":{}}, {"kind":"stuff","data":{}}]
In order to get your data, you need a loop or specify which object you want :
var obj = JSON.parse(body);
console.log(obj[0].data); // for example
Credit goes to this guy https://github.com/loganfranken
It looks like https://www.reddit.com/r/Seattle/comments/3e0q5u/why_metro_matters_infographic/.json returns an array as the top-level JSON element, so I think changing this:
obj.data.children
To this:
obj[1].data.children
Should do the trick.

Node.js TypeError: Cannot call method 'write' of undefined

This is the code in nodejs for to call the openweather API and print the result on the 127.0.0.7:8124 but do not understand why it does not work
var http = require('http');
function getData(city, res){
var urlData = 'http://api.openweathermap.org/data/2.5/weather?q='+city;
http.get(urlData, function(resi) {
var body = '';
resi.on('data', function(chunk) {
body += chunk;
});
resi.on('end', function() {
var dataResponse = JSON.parse(body)
res.write(dataResponse);
});
}).on('error', function(e) {
res.write("Got error: " + e);
});
}
// create http server
http.createServer(function (req, res) {
var query = require('url').parse(req.url).query;
var app = require('querystring').parse(query).city;
// content header
res.writeHead(200, {'Content-Type': 'text/plain'});
if(app){
console.log("ad: "+getData(app));
} else res.write("Use url:port?city=xxxx");
res.end();
}).listen(8124);
console.log('Server running at 8124');
this is the error
overflow#overflow-1015cx:~/Scrivania/nodeweather$ node app.js
Server running at 8124
ad: undefined
/home/overflow/Scrivania/nodeweather/app.js:15
res.write(dataResponse);
^
TypeError: Cannot call method 'write' of undefined
at IncomingMessage.<anonymous> (/home/overflow/Scrivania/nodeweather/app.js:15:13)
at IncomingMessage.EventEmitter.emit (events.js:117:20)
at _stream_readable.js:920:16
at process._tickCallback (node.js:415:13)
overflow#overflow-1015cx:~/Scrivania/nodeweather$
Why can not I return the result?
You are not passing the response object into getData
I believe it should look like this, but I have not tested it.
if(app){
console.log("ad: "+getData(app,res));
} else res.write("Use url:port?city=xxxx");\
If you read the error, its not telling you that you can't write, it's saying that you're trying to call write on a null object. If you trace the clues as to how res can be null, it should become clear.
Nodejs is async, res.end() is called before of res.write inside the http request.. so you need to use some "promise" tecnique or at least callbacks. However this code cannot work since you're trying to write a parsed json string, but the write method accepts only strings or buffer...moreover getData doesn't return nothing..so console.log("ad: "+getData(app,res,res.end)); prints an undefined variable.
maybe this code more fits your idea ( tested and working using "rome" ):
var http = require('http');
function getData(city, res,callback){
var urlData = 'http://api.openweathermap.org/data/2.5/weather?q='+city;
http.get(urlData, function(resi) {
var body = '';
resi.on('data', function(chunk) {
body += chunk;
});
resi.on('end', function() {
body=JSON.stringify(JSON.parse(body), null, 4)
res.write(body);
callback(body);
});
}).on('error', function(e) {
res.write("Got error: " + e);
callback("error");
});
}
// create http server
http.createServer(function (req, res) {
var query = require('url').parse(req.url).query;
var app = require('querystring').parse(query).city;
// content header
res.writeHead(200, {'Content-Type': 'text/plain'});
if(app){
getData(app,res,function (message) {
console.log("ad:",message);
res.end();
});
} else {
res.write("Use url:port?city=xxxx");
res.end("done");
}
}).listen(8124);
console.log('Server running at 8124');

Categories