Error: connect ECONNREFUSED 127.0.0.1:3002 - node.js

I'm trying to run a very simple node file that used to work, but now I get this error.
events.js:160
throw er; // Unhandled 'error' event
^
Error: connect ECONNREFUSED 127.0.0.1:3002
at Object.exports._errnoException (util.js:1026:11)
at exports._exceptionWithHostPort (util.js:1049:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1081:14)
The thing is that if I run another application in this port, there's no problem. Even If I change the port in the file, the error persists.
The code of the file:
var http = require('http');
var opcoes = {
hostname: 'localhost',
port: 3002,
path: '/',
method: 'post',
headers: {
'Accept': 'application/json',
'Content-type': 'application/json'
}
}
//Content-type
var html = 'nome=José'; //x-www-form-urlencoded
var json = { nome: 'José' };
var string_json = JSON.stringify(json);
var buffer_corpo_response = [];
var req = http.request(opcoes, function(res) {
res.on('data', function(pedaco) {
buffer_corpo_response.push(pedaco);
});
res.on('end', function() {
var corpo_responde = Buffer.concat(buffer_corpo_response).toString();
console.log(corpo_responde);
});
});
req.write(string_json);
req.end();

Your port in your code is 3005. Make sure they all share that number.

Related

SQL Server connection with Node js

var express = require('express');
var app = express();
app.get('/', function (req, res) {
var sql = require("mssql");
// config for your database
var config = {
user: 'sa',
password: '1234',
server: 'AHMAD\SQLEXPRESS',
database: 'dbms_lab4',
port:1433,
encrypt:false
};
// connect to your database
sql.connect(config, function (err) {
if (err) console.log(err);
// create Request object
var request = new sql.Request();
// query to the database and get the records
request.query('select * from Products', function (err, recordset) {
if (err) console.log(err)
// send records as a response
res.send(recordset);
});
});
});
var server = app.listen(5000, function () {
console.log('Server is running..');
});
I can't connect to SQL Server. When I run the above program and access localhost:5000, nothing appears on the browser but a message on the terminal of vscode:
"The default value for `config.options.enableArithAbort` will change from `false` to `true` in the next major version of `tedious`. Set the value to `true` or `false` explicitly to silence this message. node_modules\mssql\lib\tedious\connection-pool.js:61:23
ConnectionError: Connection is closed.
at Request._query (D:\JS\Sql_Connection\node_modules\mssql\lib\base\request.js:447:37)
at Request._query (D:\JS\Sql_Connection\node_modules\mssql\lib\tedious\request.js:346:11)
at Request.query (D:\JS\Sql_Connection\node_modules\mssql\lib\base\request.js:383:12)
at Immediate.<anonymous> (D:\JS\Sql_Connection\server.js:27:17)
at processImmediate (internal/timers.js:458:21) {
code: 'ECONNCLOSED',
name: 'ConnectionError'
}
ConnectionError: Failed to connect to AHMADSQLEXPRESS:1433 - getaddrinfo ENOTFOUND AHMADSQLEXPRESS
at Connection.<anonymous> (D:\JS\Sql_Connection\node_modules\mssql\lib\tedious\connection-pool.js:68:17)
at Object.onceWrapper (events.js:428:26)
at Connection.emit (events.js:321:20)
at Connection.socketError (D:\JS\Sql_Connection\node_modules\mssql\node_modules\tedious\lib\connection.js:1290:12)
at D:\JS\Sql_Connection\node_modules\mssql\node_modules\tedious\lib\connection.js:1116:21
at GetAddrInfoReqWrap.callback (D:\JS\Sql_Connection\node_modules\mssql\node_modules\tedious\lib\connector.js:158:16)
at GetAddrInfoReqWrap.onlookupall [as oncomplete] (dns.js:76:17) {
code: 'ESOCKET',
originalError: ConnectionError: Failed to connect to AHMADSQLEXPRESS:1433 - getaddrinfo ENOTFOUND AHMADSQLEXPRESS
at ConnectionError (D:\JS\Sql_Connection\node_modules\mssql\node_modules\tedious\lib\errors.js:13:12)
at Connection.socketError (D:\JS\Sql_Connection\node_modules\mssql\node_modules\tedious\lib\connection.js:1290:56)
at D:\JS\Sql_Connection\node_modules\mssql\node_modules\tedious\lib\connection.js:1116:21
at GetAddrInfoReqWrap.callback (D:\JS\Sql_Connection\node_modules\mssql\node_modules\tedious\lib\connector.js:158:16)
at GetAddrInfoReqWrap.onlookupall [as oncomplete] (dns.js:76:17) {
message: 'Failed to connect to AHMADSQLEXPRESS:1433 - getaddrinfo ENOTFOUND AHMADSQLEXPRESS',
code: 'ESOCKET'
},
name: 'ConnectionError'
}
ConnectionError: Connection is closed.
at Request._query (D:\JS\Sql_Connection\node_modules\mssql\lib\base\request.js:447:37)
at Request._query (D:\JS\Sql_Connection\node_modules\mssql\lib\tedious\request.js:346:11)
at Request.query (D:\JS\Sql_Connection\node_modules\mssql\lib\base\request.js:383:12)
at D:\JS\Sql_Connection\server.js:27:17
at D:\JS\Sql_Connection\node_modules\mssql\lib\base\connection-pool.js:241:7
at processTicksAndRejections (internal/process/task_queues.js:97:5) {
code: 'ECONNCLOSED',
name: 'ConnectionError'
}"
For SQL Connections: "Microsoft style strings of hostname\instancename are not supported."
EG from : https://www.w3schools.com/nodejs/nodejs_mysql.asp
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
You need to update your config accordingly.
try escaping the \ in the server name
Change
server: 'AHMAD\SQLEXPRESS'
To
server: 'AHMAD\\SQLEXPRESS',

HTTPS Get gives connection refused error while connecting to Bing API using nodejs

I was able to connect to the Bing API end point using Chrome extension - Postman.
I am new to NodeJS and AWS Lambda. Seeing the below error while connecting from an AWS Lambda function with nodejs 6.9.0
Error: connect ECONNREFUSED 127.0.0.1:443
at Object.exports._errnoException (util.js:1018:11)
at exports._exceptionWithHostPort (util.js:1041:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1086:14)
Code:
exports.handler = (event, context, callback) => {
var headers = {
'Ocp-Apim-Subscription-Key': '******************',
'Content-Type': 'application/json',
};
var options = {
url: 'https://api.cognitive.microsoft.com/bing/v5.0/search?
&q=hello',
method: 'GET',
headers: headers,
};
const https = require("https");
https.get(options);
};
Appreciate your help
You may require and use the package node-bing-api to search with Bing Api.
var Bing = require('node-bing-api')({ accKey: "your-account-key" });
Bing.web("hello", function(error, res, body){
console.log(body);
});

http request to a different app in a different port

I have 2 nodejs apps one running in port 8000 that only returns "hello"
and another app running on port 3000 that makes a simple http request to the first app
var http = require('http');
var r = http.get({
host: 'localhost',
path: '/',
port: '8000'
},
function(response) {
var body = '';
response.on('data', function(d) {
body += d;
});
response.on('end', function() {
console.log(body);
});
});
the console log returns
events.js:141
throw er; // Unhandled 'error' event
^
Error: connect ECONNREFUSED 127.0.0.1:8000
at Object.exports._errnoException (util.js:874:11)
at exports._exceptionWithHostPort (util.js:897:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1063:14)
What´s the problem here?
the first app is running correctly in http://localhost:8000/ but for some reason
when the second app makes a request to the first app I get the error I posted above. thanks for your help.
Seems like first app (on port 8000) is not reachable or not started at the moment, when second app sends request.

How can I use an https proxy with node.js https/request Client?

I need to send my client HTTPS requests through an intranet proxy to a server.
I use both https and request+global-tunnel and neither solutions seem to work.
The similar code with 'http' works. Is there other settings I missed?
The code failed with an error:
REQUEST:
problem with request: tunneling socket could not be established, cause=socket hang up
HTTPS:
events.js:72
throw er; // Unhandled 'error' event
^
Error: socket hang up
at SecurePair.error (tls.js:1011:23)
at EncryptedStream.CryptoStream._done (tls.js:703:22)
at CleartextStream.read [as _read] (tls.js:499:24)
The code is the simple https test.
var http = require("https");
var options = {
host: "proxy.myplace.com",
port: 912,
path: "https://www.google.com",
headers: {
Host: "www.google.com"
}
};
http.get(options, function(res) {
console.log(res);
res.pipe(process.stdout);
});
You probably want to establish a TLS encrypted connection between your node app and target destination through a proxy.
In order to do this you need to send a CONNECT request with the target destination host name and port. The proxy will create a TCP connection to the target host and then simply forwards packs between you and the target destination.
I highly recommend using the request client. This package simplifies the process and handling of making HTTP/S requests.
Example code using request client:
var request = require('request');
request({
url: 'https://www.google.com',
proxy: 'http://97.77.104.22:3128'
}, function (error, response, body) {
if (error) {
console.log(error);
} else {
console.log(response);
}
});
Example code using no external dependencies:
var http = require('http'),
tls = require('tls');
var req = http.request({
host: '97.77.104.22',
port: 3128,
method: 'CONNECT',
path: 'twitter.com:443'
});
req.on('connect', function (res, socket, head) {
var tlsConnection = tls.connect({
host: 'twitter.com',
socket: socket
}, function () {
tlsConnection.write('GET / HTTP/1.1\r\nHost: twitter.com\r\n\r\n');
});
tlsConnection.on('data', function (data) {
console.log(data.toString());
});
});
req.end();

Error: connect ETIMEDOUT Using node.js to download zip

I am using node.js to download a zip folder from github's server. For some reason the code below works on any other network except for at work. Not sure it's a proxy or firewall issue slowing down the request causing a timeout.
var https = require('https');
var fs = require('fs');
var options = {
hostname : 'codeload.github.com',
port : 443,
path : '/wet-boew/wet-boew/zip/master',
method : 'GET'
};
var file = fs.createWriteStream("test_folder/master.zip");
var req = https.request(options, function(res) {
console.log("statusCode: ", res.statusCode);
console.log("headers: ", res.headers);
res.on('data', function(d) {
file.write(d);
});
});
req.end();
req.on('error', function(e) {
console.error(e);
});
This is the full error that is generated when running the above code on the network:
{ [Error: connect ETIMEDOUT] code: 'ETIMEDOUT', errno: 'ETIMEDOUT', syscall: 'connect' }
any work around ideas or help is appreciated, thanks!

Resources