Node net.createConnection ECONNREFUSED - node.js

I am trying to create a Socket server, but I keep getting ECONNREFUSED when I try and launch the server.
var net = require('net');
var server = net.createConnection(3000,'localhost',function(socket) {
console.log('connected!');
});
server.on('error', function(e) {
console.log(e);
});
Nothing fancy.
However when I try to run it node server.js, I only get an error message. Is there a step I am missing here? Is it maybe a configuration issue?
I am running node 0.10.32 if that helps.
Thanks

That error is your client cant connect to server. You can add this code to snip error.
server.on('error', function(err){
console.log(err.message);
});

Did your server start ? You can use http.createServer to create server listen on port 3000 and you connect to it.

Related

redis is working in local but not working in server

I have a redis server which is working fine in my local but in ubuntu server is not working can someone gives the comment for installing redis in server
it is not working even with docker it is working only while i am running in local
const redis=require('redis');
var redisClient:any;
(async () => {
try {
redisClient = redis.createClient({ socket: { port: 6379 } });
await redisClient.connect();
// const redisClient = redis.createClient({
// port:"6379",
// host:'redis-service'
// });
redisClient.on('connect',()=>{
console.log('server connected to redis')
})
redisClient.on('ready',()=>{
console.log('Client Connect to redis and ready to use')
})
redisClient.on('error',(err:any)=>{
console.log(err)
})
redisClient.on('end',()=>{
console.log('Server disconnected from redis')
})
process.on('SIGINT',()=>{
redisClient.quit()
})
console.log('connected');
} catch (err) {
console.error(err)
}
})()
export{
redisClient
};
Make sure that Redis is not already running on the server. You can check this by running the command ps aux | grep redis. If Redis is running, you should see a line with the redis-server process.
Confirm that Redis is properly installed on your server by running redis-cli ping. If Redis is installed and running, it should return "PONG"
Check the Redis configuration file (redis.conf) and ensure that the IP and port settings match the settings on your local machine.
Make sure that the Redis server has the necessary permissions to access its data directory and that the correct ownership and permissions are set on its files.
Verify that the firewall rules on the server allow incoming connections on the Redis port (usually 6379)
Check for any compatibility issues between the version of Redis that you're running on your local machine and the version of Redis that's running on the Ubuntu server. If there's a version mismatch, it may be necessary to upgrade or downgrade one of the installations.
Try to run the Redis server with verbose output by running the command redis-server -v, this can give you some more information about the errors that are causing the server to fail.
Look at Redis log files, usually located in /var/log/redis for further clues about the cause of the problem.

connecting node js to postgresql on heroku

I have deployed my app on heroku and was able to make a connection to the database from pg:psql in my terminal by copying and pasting the code provided by heroku. Now i want to directly connect my node app to postgres on heroku but no success. I followed the instruction on heroku on how to connect nodejs to postgres here.. https://devcenter.heroku.com/articles/heroku-postgresql#connecting-in-node-js but i can't seem to get it to work. I simply installed pg module and pasted the block of code provided by heroku inside my get request in my server.js file.
app.use(express.static('public'));
app.use(bodyParser.json());
app.get('/food', function(req, res) {
pg.defaults.ssl = true;
pg.connect(process.env.DATABASE_URL, function(err, client) {
if (err) throw err;
console.log('Connected to postgres! Getting schemas...');
client.query('SELECT table_schema,table_name FROM information_schema.tables;').on('row', function(row) {
console.log(JSON.stringify(row));
});
});
});
http.listen(process.env.PORT || 3000, function(){
console.log('listening to port 3000!');
});
the error i get from heroku logs is..
/appserver.js:53
if (err) throw err;
Error: connect ECONNREFUSED 127.0.0.0.1:5432
after searching for that error message, i get some people suggesting on fixing or changing the DATABASE_URL. How and where do i change it? I'm lost. I would really appreciated if i can get some help to fix it.
The pg connection is using the database url in your environment:
pg.connect(process.env.DATABASE_URL, function(err, client) {}
Because the DATABASE_URL env variable is not set, it defaults to localhost 127.0.0.0.1:5432. You can set DATABASE_URL in your bashrc or bash_profile. You can name it more specifically PROJECTNAME_DATABASE_URL if you have multiple apps. On Heroku, you can set it like this:
$ heroku config:set DATABASE_URL=postgres://user:password#host:port/database

Connection to MongoDb failed

I was trying to connect my mongodb with node server using command prompt.
I started mongodb my mongod --dbpath E:\node start\node\data
Then I installed mongodb dependencies using npm install mongodb
I added some code into my app.js which is described below :
app.js
var mongodb = require('mongodb'); //acquiring mongodb native drivers
var mongoClient = mongodb.MongoClient;
var url = 'mongodb://localhost:7000/myDatabase'; //connection url
mongoClient.connect(url, function(err,db){
if(err){
console.log('Unable to connect to mongodb server. Error :' , err);
}
else{
console.log('Connection established to', url);
db.close();
}
});
when I ran app.js in command prompt, following error occured :
Unable to connect to mongodb server. Error :{[ MongoError : connect ECONNREFUSED] name : 'MongoError' , message: 'connect ECONNREFUSED' }
I cannot understand what the problem is and what should I do next.
MongoDB usually runs on port 27017, but you're trying to connect to port 7000. Try changing your url variable.
var url = 'mongodb://localhost:27017/myDatabase';
You know mongoDB has their default port no 27017.
And You have written 7000.
So Try to Change port no to 27017.
ok !!!!!!!
The error says you do not have mongodb running. You should check if your mongodb is running or not. If its running then you should check on what port it is running on.
The default port for mongodb is 27017. If you have not configured your mongodb to run on port 7000 then changing var url = 'mongodb://localhost:7000/myDatabase'; to var url = 'mongodb://localhost:27017/myDatabase'; will work for you.

Can't Connect To A Telnet Server From Node.js

I have a telnet server embedded in a C++ app that I can connect to with no problem
using telnet.
I want to write a node application that will connect to my server and I have tried
this
var net = require('net');
var port = 6502
var host = '127.0.0.1'
var socket = net.connect(port,host, function() {
console.log("Sending data");
socket.write("hello\r\n")
socket.on("data", function (data) {
console.log("received data");
console.log( data.toString() );
socket.end();
})
})
socket.on("error", function(err) {
console.log("Error");
console.log(err);
})
Unfortunately what I get back is this
> node test.js
{ [Error: connect ECONNREFUSED]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect' }
What's really odd is if I set up a simple echo server with node everything works
fine. Here's the working echo server code:
var net = require('net');
var server = net.createServer(function (socket) {
socket.write('Echo server\r\n');
socket.pipe(socket);
});
server.listen(6502, '127.0.0.1');
and from that I get
Sending data
received data
Echo server
hello
Is there any reason why:
I can telnet into my app fine
I can connect from node to my node echo server on the same port
I get a connection refused if I connect from my node app to my app
Extra Info
On OSX (mavericks)
Node version 0.10.28
Telnet server in C++ is provided via embedded lua and luasocket (lua 5.1)
Solved!
The issue is the code in my app server was binding to localhost and by default
that binds to the IPV6 address of ::1
Passing a host of localhost to net.connect assumes IPV4 and doesn't work.
The mac command line telnet and nc both work fine with this and connect correctly.
Two solutions:
App binds to 127.0.0.1 and localhost in node works fine
Set host address to ::1 in test.js and it connects via ipv6
All fixed now though :)
gaz
Looks like you forgot to tell the server to listen in your code. It throws a connection refused error because there is nothing to connect to...
Add this at the end: server.listen(port);
The prototype for net.connect is (options, callback)
See http://nodejs.org/api/net.html#net_net_connect_options_connectionlistener
I would then suggest to test your code against a standard telnet server to see how it behaves, and finally I would strongly recommend the use of jshint or jslint.

Node JS Net.Connect ECONNREFUSED

I created a very simple Node.JS script which is suppose to connect to a server on port 3200. The code works great on Windows 8.1 x64, but when I moved it to a Ubuntu Server machine I am getting a ECONNREFUSED.
I tried to connect via IP Address and the Domain Name, but neither work. It always gives me ECONNREFUSED.
When I SSH into the Ubuntu Server I can do a telnet to the server/port and it works fine. Also pinging the server works. Just Node.JS doesnt connect.
Server:
Ubuntu Server 12.04.3 LTS (precise)
Here is the code:
var net = require('net');
var socket = net.connect({port: 3200, host: '172.19.16.203'}, function() {
// Connection Successful
console.log('Socket connected...');
// Data Received from Server
socket.on('data', function(d) {
console.log('data');
});
socket.on('end', function() {
});
socket.on('error', function(e) {
});
});
I found the solution. I was running my script by the following command:
node test.js
I guess the node is an old version. I upgraded the OS to the latest version and then did:
apt-get install nodejs
I run the script like following:
nodejs test.js
And it works!

Resources