Error: connect ECONNREFUSED node.js https.get request - javascript

I'm running the following in a small test node project.
var https = require('https');
var request = https.get('https://example.com/script.json', function(response){
console.dir(response);
});
request.on('error', function(){
console.log(err);
});
When I try to console.dir the response I get the following error.
throw er; // Unhandles 'error' event
Error: connect ECONNREFUSED xxx.xx.xxx.xx:xxx
It's a simple get request to a json file on an external server. I know the file exists so I'm not sure why I'm getting the above error. The file can be run in the browser and requires no authentication.
UPDATE: I edited the code. created a promise and added an .on('error'... to catch any issues.
The following is now output:
( [Error: connect ECONNREFUSED xxx.xx.xxx.xx:xxx]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: 'xxx.xxx.xxx.xxx',
port: 443 )

Thanks to help from #robertklep I came up with the following solution which allows me to connect to HTTPS from behind a proxy using request
const https = require('https');
const request = require('request');
request(
{
'url':'https://https://example.com/script.json',
'proxy':'http://xx.xxx.xxx.xx'
},
function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
}
);

The other server is not up, or not listening to 443 port so you get a connection refused.
Another option is that the nodejs app is resolving that example url differently to a different ip than your pc.

Related

Example node.js ftp server?

I need to create a node.js app that connects to this ftp server and downloads files from this directory:
ftp://www.ngs.noaa.gov/cors/rinex/2021/143/nynb
I've tried following the ftp npm package docs but I feel like I am doing something horribly wrong:
import Client from "ftp";
/**
* https://github.com/mscdex/node-ftp
*/
const c = new Client();
c.on("ready", function () {
c.get(
"ftp://www.ngs.noaa.gov/cors/rinex/2021/143/nynb",
(error, stream) => {
if (error) throw error;
console.log(`stream`, stream);
stream.once("close", function () {
c.end();
});
}
);
});
// connect to localhost:21 as anonymous
c.connect();
When I run npm run dev with nodemon I get:
Error: connect ECONNREFUSED 127.0.0.1:21
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
[nodemon] app crashed - waiting for file changes before starting...
Can someone please help? I'm completely stumped.
Is it possible if someone could show me a small example of how I can connect to this remote ftp server?
There are a few points :
You're connecting to the local ftp with c.connect();. You need to connect to www.ngs.noaa.gov to download files from there.
This path cors/rinex/2021/143/nynb is a directory on the remote host. c.get doesn't work, you need to list all files in the directory then download them 1 by 1.
The code below connect to the remote server and list all files in the directory
const Client = require('ftp');
const fs = require("fs");
const c = new Client();
c.on('ready', function () {
c.list( "/cors/rinex/2021/143/nynb", function (err, list) {
if (err) throw err;
console.dir(list);
});
});
c.connect({
host: "www.ngs.noaa.gov",
});

Connection cannot be established to FTP server using nodejs

I´m currently trying to connect to a FTP server using the nodeJS lib basic-ftp. It seems that a connection was established, but I cannot perform a single operation or even output a log statement after trying to access the server. The code then runs into an timeout.. I can ping the server from my local environment using Windows CMD ping command..
const ftp = require("basic-ftp")
example()
async function example() {
const client = new ftp.Client(timeout = 30000)
client.ftp.verbose = true
try {
await client.access({
host: "10.xxx.xx.xx",
port: 2222,
user: "xxx",
pass: "xxxx",
secure: true
})
console.log("CONNECTED");
console.log(await client.ensureDir("/PATH/"));
console.log(await client.list('/PATH/'));
}
catch (err) {
console.log(err)
}
client.close()
}
Output:
Connected to 10.xxx.xx.xxx:2222 (No encryption)
< SSH-2.0-WS_FTP-SSH_8.5.0
Error: Timeout (control socket)
at Socket.<anonymous> (C:\Users\xxx\dev\nodejs\ftpTest\node_modules\basic-ftp\dist\FtpContext.js:316:58)
at Object.onceWrapper (events.js:299:28)
at Socket.emit (events.js:210:5)
at Socket._onTimeout (net.js:469:8)
at listOnTimeout (internal/timers.js:531:17)
at processTimers (internal/timers.js:475:7)
There is almost no output that helps me to identify the issue. I can establish a connection using WinSCP.
Does someone have an idea what could cause the problem and which steps are required to identify what could cause this issue?
Thanks a lot!
EDIT:
Could it be that there is some kind of key exchange missing? I had to click yes when using WinSCP!

Nodemailer - SMTP Timeout - office365

I am trying to use SMTP connection with nodemailer with office365 mail.
When I try to send mail, it throws up this error -
Error: connect ETIMEDOUT xx.xxx.xxx.xxx:587
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1161:14)
errno: 'ETIMEDOUT',
code: 'ECONNECTION',
syscall: 'connect',
address: 'xx.xxx.xxx.xxx',
port: 587
It is not a proxy issue, since I tried outside of the network and it throws up the same error as well.
`
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
host: 'smtp.office365.com',
secure: false,
auth: {
user: "xxxxx#xxx.com",
pass: "xxxxx"
},
tls: {
ciphers: 'SSLv3'
}
});
var message = {
from: 'Sender Name <xxx#xxx.com>',
to: '"Receiver Name" <xxx#xxx.com>',
subject: 'Nodemailer',
text: 'Hello to myself!'
};
transporter.sendMail(message, function(error, response) {
if (error) {
console.log('Error occured');
res.end("<html><body><h1>error</h1></body></html>");
console.log(error);
return;
} else {
console.log(response);
res.end("<html><body><h1>success</h1></body></html>");
console.log('Message sent successfully!');
}
});
`
I tried with other services as well like Gmail, it also gives the same error inside and outside the network.
Do you have a local firewall or anti-virus product that does port blocking? My company routinely blocks computers from making outbound connections on common SMTP ports because it was a common multi-mailer virus propagation path. smtp.office365.com responds on both port 25 and 587 from a host I know to be able to initiate outbound SMTP traffic.
[~]# nmap -p25,587 smtp.office365.com
Starting Nmap 7.60 ( https://nmap.org ) at 2018-12-04 10:39 EST
Nmap scan report for smtp.office365.com (40.97.170.178)
Host is up (0.027s latency).
PORT STATE SERVICE
25/tcp open smtp
587/tcp open submission
this might be a bit late but this is more for anyone else in the future.
Common cause for timeouts is you have to enable SMTP, if using a mac this can be done through PowerShell then look to here. If youre on a mac, you'll have to install WSman because it doesnt come with powershell. run on port 587. set secure to false. then make sure your config email and the email in your auth and data for postman are the same.

node.js Error: connect ECONNREFUSED; response from server

I have a problem with this little program:
var http = require("http");
var request = http.request({
hostname: "localhost",
port: 8000,
path: "/",
method: "GET"
}, function(response) {
var statusCode = response.statusCode;
var headers = response.headers;
var statusLine = "HTTP/" + response.httpVersion + " " +statusCode + " " + http.STATUS_CODES[statusCode];
console.log(statusLine);
for (header in headers) {
console.log(header + ": " + headers[header]);
}
console.log();
response.setEncoding("utf8");
response.on("data", function(data) {
process.stdout.write(data);
});
response.on("end", function() {
console.log();
});
});
The result in console is this:
events.js:141
throw er; // Unhandled 'error' event
^
Error: connect ECONNREFUSED 127.0.0.1:8000
at Object.exports._errnoException (util.js:870:11)
at exports._exceptionWithHostPort (util.js:893:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1063:14)
I do not understand why this happens.
From your code, It looks like your file contains code that makes get request to localhost (127.0.0.1:8000).
The problem might be you have not created server on your local machine which listens to port 8000.
For that you have to set up server on localhost which can serve your request.
Create server.js
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Hello World!'); // This will serve your request to '/'.
});
app.listen(8000, function () {
console.log('Example app listening on port 8000!');
});
Run server.js : node server.js
Run file that contains code to make request.
Please use [::1] instead of localhost, and make sure that the port is correct, and put the port inside the link.
const request = require('request');
let json = {
"id": id,
"filename": filename
};
let options = {
uri: "http://[::1]:8000" + constants.PATH_TO_API,
// port:443,
method: 'POST',
json: json
};
request(options, function (error, response, body) {
if (error) {
console.error("httpRequests : error " + error);
}
if (response) {
let statusCode = response.status_code;
if (callback) {
callback(body);
}
}
});
I solved this problem with redis-server, you can install that like this!
sudo apt-get install redis-server
after that see the port and change it!
I had the same problem on my mac, but in my case, the problem was that I did not run the database (sudo mongod) before; the problem was solved when I first ran the mondo sudod on the console and, once it was done, on another console, the connection to the server ...
Just run the following command in the node project:
npm install
Its worked for me.
i ran the local mysql database, but not in administrator mode, which threw this error
If you have stopped the mongod.exe service from the task manager, you need to restart the service. In my case I stopped the service from task manager and on restart it doesn't automatically started.
I got this error because my AdonisJS server was not running before I ran the test. Running the server first fixed it.
If this is the problem with connecting to the redis server (if your redis.createClient function does not work although you are sure that you have written the right parameters to the related function), just simply type redis-server in another terminal screen. This probably gonna fix the issue.
P.S.: Sorry if this is a duplicate answer but there is no accepted answer, so, I wanted to share my solution too.
This is very slight error. When I was implementing Event server between my processes on nodejs.
Just check for the package you're using to run your server like Axios.
Maybe you might have relocated the files or disconnected some cache data on the browsers.
It's simple ,In your relevant directory run the following command
I was using axios so I used
npm i axios
Restart the server
npm start
This will work.
use a proxy property in your code it should work just fine
const https = require('https');
const request = require('request');
request({
'url':'https://teamtreehouse.com/chalkers.json',
'proxy':'http://xx.xxx.xxx.xx'
},
function (error, response, body) {
if (!error && response.statusCode == 200) {
var data = body;
console.log(data);
}
}
);

Http-proxy hangs up all the time

I have a JavaScript proxy server that often hangs up after having used it a while. This is the proxy code:
var express = require(["express"], function(){}),
http = require(["http"], function(){}),
port = (process.env.PORT || 8001),
server = module.exports = express(),
httpProxy = require(['http-proxy'], function(){});
var proxy = httpProxy.createProxyServer();
// SERVER CONFIGURATION
// ====================
server.configure(function() {
server.use(function(req, res, next) {
if (req.url.indexOf('/any/thing') === 0) {
//console.log(res);
proxy.web(req, res, {target: 'http://any.thing.com'});
} else {
next();
}
});
server.use('/anything', express["static"](__dirname + "/../public"));
server.use(express.errorHandler({
dumpExceptions: true,
showStack: true
}));
server.use(express.bodyParser());
server.use(server.router);
});
// Start Node.js Server
http.createServer(server).listen(port);
I am trying to do some tests with Nightwatch.js. The tests work up to a point, then the server crashes. In some tests this point is always reached at the same time, in others it depends when the server crashes and if it crashes at all.
This is the Error message:
C:...\node_modules\http-proxy\lib\http-proxy\index.js:119
throw err;
^
Error: socket hang up
at createHangUpError (_http_client.js:215:15)
at Socket.socketCloseListener (_http_client.js:247:23)
at Socket.emit (events.js:129:20)
at TCP.close (net.js:485:12)
Stopping Express server
What could be the reason for this? I was not able to figure it out in google.
The error is thrown when parallelly sending requests to the http-proxy.
The error can be prevented by installing a different version of http-proxy.
For me the error occured in http-proxy version 1.6.2.
I fixed the problem by installing version 1.0.0:
npm uninstall http-proxy
then
npm install http-proxy#1.0.0

Categories