Error: connect ETIMEDOUT while connecting to host - node.js

I am new to nodejs and socket.io i try to make a simple chat through socket.io, connecting with my database.But have an issue while am connecting to host using node.js. I am using amazon server. Please help me to solve this.
This is my code :-
var bodyParser = require('body-parser');
var express = require('express');
var config = require('./config/config');
var io = require('socket.io')();
var openDB = require('json-file-db');
var db = openDB('db.json');
var mysql = require('mysql');
var gcm = require('node-gcm-service');
var apn = require('apn');
var app = express();
app.listen(config.HTTP_PORT); // 3000
app.use(bodyParser.json());
io.listen(config.SOCKET_PORT);//7777
console.log("Server is up");
var connection = mysql.createConnection({
host : 'xx.xx.xxx.xxx',
user : 'root',
password : 'my_password',
database : 'my_db'
});
connection.connect(function(error){
if(!!error)
{
console.log(error);
console.log('Error');
}
else
{
console.log('connected');
}
});
Error: connect ETIMEDOUT
at Connection._handleConnectTimeout (/var/www/html/server1/node_modules/mysql/lib/Connection.js:425:13)
at Socket.g (events.js:286:16)
at emitNone (events.js:86:13)
at Socket.emit (events.js:185:7)
at Socket._onTimeout (net.js:333:8)
at tryOnTimeout (timers.js:228:11)
at Timer.listOnTimeout (timers.js:202:5)
--------------------
at Protocol._enqueue (/var/www/html/server1/node_modules/mysql/lib/protocol/Protocol.js:141:48)
at Protocol.handshake (/var/www/html/server1/node_modules/mysql/lib/protocol/Protocol.js:52:41)
at Connection.connect (/var/www/html/server1/node_modules/mysql/lib/Connection.js:136:18)
at Object. (/var/www/html/server1/app1.js:35:16)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Module.runMain (module.js:575:10)
errorno: 'ETIMEDOUT',
code: 'ETIMEDOUT',
syscall: 'connect',
fatal: true

You need to open remote mysql port (3306 by default).

Related

Having issue while connecting NodeJS with SQL

since 2 weeks I am trying to connect Microsoft SQL server with NodeJS. Please help me out with this
Am available on Instagram : shaziyali8
Thanks in advance
Here is my Code#
const mysql = require("mysql");
const connection = mysql.createConnection({
host: "HP\SQLEXPRESS", // I tried things such as : localhost, Id address: 127.0.0.1, . (a dot)
user: "root",
password: "",
database: "testEmp",
// port: 8889,
});
// console.log(connection);
connection.connect((err) => {
if (err) {
console.log(err);
} else {
console.log("connect");
}
});
Got error
Error: getaddrinfo ENOTFOUND HPSQLEXPRESS
at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:71:26)
--------------------
at Protocol._enqueue (F:\NodeJs\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (F:\NodeJs\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at Connection.connect (F:\NodeJs\node_modules\mysql\lib\Connection.js:116:18)
at Object.<anonymous> (F:\NodeJs\MySql\index.js:13:12)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
at node:internal/main/run_main_module:17:47 {
errno: -3008,
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'HPSQLEXPRESS',
fatal: true
}
From what I can see, you are trying to connect to a Microsoft SQL server but you are using a MySQL client npm package for the connection. The correct package to connect to a MSSQL server in Node.js is mssql
Install this and check if the connection issue still persists. Also, if the MSSQL server is on your local then using localhost as host name with correct port number would work most of the time.
There are also Connection Strings that you can use to connect to the MSSQL server.
See the documentation here

How to run my app.js on random ports for running the node js we application

I am facing a trouble some problem where, I need kill everytime a given port to run my app.js which is front end for node js and
here is my app.js
var express = require("express");
var path = require("path");
//var routes = require("./routes");
var app = express();
app.set("port", process.env.PORT || 3000);
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "ejs");
app.use("/public",express.static(path.resolve(__dirname,"public")));
app.use("/", require("./routes/web"));
app.use("/api", require("./routes/api"));
app.listen(app.get("port"), function(){
console.log("Server started on port " + app.get("port"));
})
> coedataapp#1.0.0 build
> node app
node:events:505
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::3000
at Server.setupListenHandle [as _listen2] (node:net:1372:16)
at listenInCluster (node:net:1420:12)
at Server.listen (node:net:1508:7)
at Function.listen (/Users/xxxxxx/Documents/my-learning/coe-central/node_modules/express/lib/application.js:635:24)
at Object.<anonymous> (/Users/xxxxx/Documents/my-learning/coe-central/app.js:17:5)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
Emitted 'error' event on Server instance at:
at emitErrorNT (node:net:1399:8)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
code: 'EADDRINUSE',
errno: -48,
syscall: 'listen',
address: '::',
port: 3000
}
Added my app.js code as well as the error I see in the console log. This will save my time and I will be able to make this serverless in future.

Error: listen EACCES: permission denied 8085;

I tried running the backend server but when I run the command node backend/server.js in my cmd, I'm getting this error:
D:\theshoppingcart>node backend/server.js
events.js:187
throw er; // Unhandled 'error' event
^
Error: listen EACCES: permission denied 8085;
at Server.setupListenHandle [as _listen2] (net.js:1284:19)
at listenInCluster (net.js:1349:12)
at Server.listen (net.js:1448:5)
at Object.<anonymous> (D:\theshoppingcart\backend\server.js:16:8)
at Module._compile (internal/modules/cjs/loader.js:959:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
at Module.load (internal/modules/cjs/loader.js:815:32)
at Function.Module._load (internal/modules/cjs/loader.js:727:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)
at internal/main/run_main_module.js:17:11
Emitted 'error' event on Server instance at:
at emitErrorNT (net.js:1328:8)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
code: 'EACCES',
errno: 'EACCES',
syscall: 'listen',
address: '8085;',
port: -1
}
this is my backend/server.js file:
require('dotenv').config()
const express = require("express");
const connectDB = require('./config/db');
connectDB();
const app = express();
const PORT = process.env.PORT || 8085;
app.listen(PORT, ()=> console.log(`server running on port ${PORT}`));
I had the same issue, you should remove the semicolon ; from the .env file. That has worked for me.
Open the .env file and check ";".
PORT = 5050;
You need to remove the ";" from the .env file. make it:
PORT = 5050

Error in running server via node and express

i am still new in backend but i am facing an error since yesterday whenever i try to run my server. BELOW IS MY CODE AND ERROR
const express = require("express");
const app = express();
PORT = 8443;
app.listen("PORT", () => {
console.log("Server up and running")
});
AND HERE IS MY ERROR
events.js:288
throw er; // Unhandled 'error' event
^
Error: listen EACCES: permission denied PORT
←[90m at Server.setupListenHandle [as _listen2] (net.js:1292:21)←[39m
←[90m at listenInCluster (net.js:1357:12)←[39m
←[90m at Server.listen (net.js:1456:5)←[39m
at Function.listen (C:\Users\AbTorres9\Desktop\YelpCamp\node_modules\←[4mexpress←[24m\lib\application.js:618:24)
at Object.<anonymous> (C:\Users\AbTorres9\Desktop\YelpCamp\app.js:6:5)
←[90m at Module._compile (internal/modules/cjs/loader.js:1158:30)←[39m
←[90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)←[39m
←[90m at Module.load (internal/modules/cjs/loader.js:1002:32)←[39m
←[90m at Function.Module._load (internal/modules/cjs/loader.js:901:14)←[39m
←[90m at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)←[39m
Emitted 'error' event on Server instance at:
←[90m at emitErrorNT (net.js:1336:8)←[39m
←[90m at processTicksAndRejections (internal/process/task_queues.js:84:21)←[39m {
code: ←[32m'EACCES'←[39m,
errno: ←[32m'EACCES'←[39m,
syscall: ←[32m'listen'←[39m,
address: ←[32m'PORT'←[39m,
port: ←[33m-1←[39m
}
[nodemon] app crashed - waiting for file changes before starting...
const express = require("express");
const app = express();
const PORT = 8443;
app.listen(PORT, () => {
console.log("Server up and running")
});
you had some errors first of all you did not initialized the PORT and second you passed PORT as string in app.listen()
You are missing a view normal JavaScript things. Like defining port. And the .listen function takes in the port variable rather than a string saying "PORT"
See working example:
const express = require('express');
const app = express();
const port = 8443;
app.listen(port, () => console.log(`Server running on port ${port}!`));
#Abhishek, I think on which port are you using that is block by your environment kindly allow it on the firewall or just disable the firewall and access again it works for you!!
You should use the variable and not the string in the app.listen() function. Try to use something like this:
app.listen(PORT, () => {
console.log("Server up and running")
});

ActiveDirectory Authentication using NODE for Windows SQL 2012

Am using node Active directory for validating users. Here is my node code:
const express = require('express');
const async = require('async');
const activedirectory = require('activedirectory');
var underscore = require('underscore');
const ldap = require('ldapjs');
const bunyan = require('bunyan');
const app = express();
app.get('/',(req,res) => {
var config = { url: 'ldap://10.100.1.10',
baseDN: 'dc=HBCAudit,dc=COM',
username: 'traveldesk.dev',
password: '$oftvision#123' }
var ad = new activedirectory(config);
});
//Port Number
const port = 7000;
//CORS Middleware
app.use(cors());
//Start Server
app.listen(port, () =>{
console.log('Server started on port '+ port);
});
But am getting error in command prompt like this:
c:\spitravel>node app.js
module.js:471
throw err;
^
Error: Cannot find module 'once'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (c:\spitravel\node_modules\ldapjs\lib\client\client.js
:8:12)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
I don't know why am getting this error. Am new to NODEJS.
Its due to depencies of ldapjs module, please run npm install ldapjs and recheck

Resources