C:\Users\fc\Desktop\js-basics\Projects>node net.js
Gives this error:
Error: connect ECONNREFUSED 127.0.0.1:8085
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1056:14) {
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 8085
}
Two ways you can solve this issue.
1.You can kill port which is listening on 8085 sudo killall node
2.You can change the port number. Here's a working example:
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!');
});
Related
I'm using node.js to create connect to server on EC2.
The client.js is on the physical machine using a function http.get to get to the server.js on EC2, which using the express.get to connect.
When I ran the two file on the EC2 and my physical machine, nothing happened. And I had the error of Error: connect ETIMEDOUT.
Am I using the wrong ip address? What I use is the public Ipv4 ip.
server.js on EC2 with Ipv4 add: 100.27.18.131
const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => {
calc = 0
for (i=0;i<10000;i++) {
calc += Math.random() * Math.random();
}
console.log(calc);
res.send(calc.toFixed(10));
});
app.listen(port, () => {
console.log(`listening on port ${port}`);
});
client.js on pyhsical machine:
const http = require('http');
setInterval(loadtest, 100); //time is in ms
function loadtest()
{
http.get('http://100.27.18.131:3000', (res) => {
res.on('data', function (chunk) {
console.log('' + chunk);
});
});
}
Error:
node:events:505
throw er; // Unhandled 'error' event
^
Error: connect ETIMEDOUT 100.27.18.131:3000
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1187:16)
Emitted 'error' event on ClientRequest instance at:
at Socket.socketErrorListener (node:_http_client:454:9)
at Socket.emit (node:events:527:28)
at emitErrorNT (node:internal/streams/destroy:157:8)
at emitErrorCloseNT (node:internal/streams/destroy:122:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
errno: -4039,
code: 'ETIMEDOUT',
syscall: 'connect',
address: '100.27.18.131',
port: 3000
}
Did not set up the port for 3000 or 80 at the vary beginning.
//in my .env file
MOVIEREVIEWS_DB_URI=mongodb+srv://driiisdev:password123#cluster1.ektx7.mongodb.net/sample_mflix?retryWrites=true&w=majority
PORT=8000 //starting port of server
//in my index.js file
import app from "./server.js"
import mongodb from "mongodb"
import dotenv from "dotenv"
dotenv.config();
const uri = process.env.MOVIEREVIEWS_DB_URI;
const client = new mongodb.MongoClient(uri);
const port = process.env.PORT||5000;
(async ()=>{
try{
//Connect to the MongoDB cluster
await client.connect();
app.listen(port, ()=>{
console.log("server is running on port:" +port);
})
}catch(e){
console.error(e);
process.exit(1)
}
})().catch(console.error);
//in my server.js file
import express from 'express'
import cors from 'cors'
import movies from './api/movies.route.js'
const app = express()
app.use(cors())
app.use(express.json())
app.use("/api/v1/movies", movies)
app.use('*', (req,res)=>{
res.status(404).json({error:"not found"})
})
export default app
// on running nodemon server , the error i get:
Error: listen EACCES: permission denied 8000 //starting port of server
at Server.setupListenHandle [as _listen2] (node:net:1317:21)
at listenInCluster (node:net:1382:12)
at Server.listen (node:net:1480:5)
at Function.listen (C:\xampp\htdocs\driiisdev\movie-reviews\backend\node_modules\express\lib\application.js:635:24)
at main (file:///C:/xampp/htdocs/driiisdev/movie-reviews/backend/index.js:19:13)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
Emitted 'error' event on Server instance at:
at emitErrorNT (node:net:1361:8)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
code: 'EACCES',
errno: -4092,
syscall: 'listen',
address: '8000 //starting port of server',
port: -1
}
I am connecting to a remote Redis using the following code. The problem connecting because it does not accept the giving IP instead points to localhost.
const redisURL = "redis://20.213.158.211:6379" // random remote IP address
const client = redis.createClient({redisURL});
client.on('connect', function(){
client.select(5);
console.log('Connected to Redis');
});
client.on("error", (err) => console.log("Redis Client Error", err));
client.connect();
It throws the following error
Redis Client Error Error: connect ECONNREFUSED 127.0.0.1:6379
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 6379
}
The problem is it still trying to connect to 127.0.0.1(localhost) instead of the giving IP address above.
Unable to connect to the SMTP server. Is there a better way to connect and check the validity of the SMTP server from the domain?
var net = require('net');
const dns = require('dns')
const SMTPConnection = require("nodemailer/lib/smtp-connection");
dns.resolveMx('gmail.com', function(err, addresses){
if(err) console.log(err);
// just taking the first mail server
h = addresses[0].exchange;
options = {
host:h,
port:25,
connectionTimeout: 3000
};
let connection = new SMTPConnection(options);
connection.connect(()=>{console.log('connected')});
connection.on('error', (err)=>{console.log(err)})
connection.on('end', ()=>{console.log('end')})
})
Error
{ Error: connect EHOSTUNREACH 142.250.11.26:25
at Object._errnoException (util.js:1022:11)
at _exceptionWithHostPort (util.js:1044:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1198:14)
code: 'ESOCKET',
errno: 'EHOSTUNREACH',
syscall: 'connect',
address: '142.250.11.26',
port: 25,
command: 'CONN' }
end
I get the same error when I try with new net.Socket([options]) from the net module
I deployed my nodejs 10.16.3 app to ubuntu 18.04 from my win10 PC development. The problem is that the process.env becomes undefined on ubuntu server. The dotenv module is used as:
require('dotenv').config({path: process.cwd() +'/config/.env'});
The server is listening to:
const port = process.env.PORT; // 3000;
console.log(process.env);
server.listen(port, () => {
console.log(`env var: ${process.env.jwtPrivateKey}`)
console.log(`Listening on port ${port}...`);
});
There is a .env file under myproj\config\ storing all the user defined params. Her is a portion of the file:
PORT = 3000
DB_PASSWORD = mydbpassword
jwtPrivateKey = myprivatekey
jwt_token_expire_days = 24
jwt_secret_len = 10
vcode_time_elapse = 10
After starting the nodejs app with :
pm2 start /ebs/www/myapp/index.js
Here is printout from index-out.log:
$cat index-out.log
env var: undefined
Listening on port undefined...
Here is the index-error.log:
$ cat index-error.log
Unable to connect to the database: { SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:5433
at connection.connect.err (/ebs/www/emps/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:170:24)
at Connection.connectingErrorHandler (/ebs/www/emps/node_modules/pg/lib/client.js:174:14)
at Connection.emit (events.js:198:13)
at Socket.reportStreamError (/ebs/www/emps/node_modules/pg/lib/connection.js:72:10)
at Socket.emit (events.js:198:13)
at emitErrorNT (internal/streams/destroy.js:91:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
name: 'SequelizeConnectionRefusedError',
parent:
{ Error: connect ECONNREFUSED 127.0.0.1:5433
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:14)
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 5433 },
original:
{ Error: connect ECONNREFUSED 127.0.0.1:5433
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:14)
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 5433 } }
It must be something related to configuration of dotenv module.
Change your require dotenv statement from
require('dotenv').config({path: process.cwd() +'/config/.env'});
to
require('dotenv').config({path: __dirname +'/config/.env'});
Since the project is started from different location using below command, it is not able to read the .env file:
pm2 start /ebs/www/myapp/index.js
Difference between process.cwd() and __dirname :
process.cwd() returns the current working directory, the directory from which you invoked the node command.
__dirname returns the directory name of the directory containing the JavaScript source code file