Having issue while connecting NodeJS with SQL - node.js

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

Related

EPERM error while trying to read fullchain.pem

I'm trying to host an HTTPS website with Node.js on my Windows 10 computer, but Node.js has suddenly stopped being able to read the fullchain.pem I obtained using Let's Encrypt's Certbot.
This is my Node.js code:
const fs = require("fs")
const https = require("https")
const FS_OPTIONS = {
encoding: "utf-8",
flag: "r",
}
const key = fs.readFileSync("C:/Certbot/live/subdomain.example.com/privkey.pem", FS_OPTIONS)
const cert = fs.readFileSync("C:/Certbot/live/subdomain.example.com/fullchain.pem", FS_OPTIONS)
https.createServer({ key, cert }, (req, res) => {
// do stuff...
}).listen(8888)
However, when I run it, I receive the following error:
Error: EPERM: operation not permitted, open 'C:/Certbot/live/subdomain.example.com/fullchain.pem'
at Object.openSync (fs.js:476:3)
at Object.readFileSync (fs.js:377:35)
at Object.<anonymous> (D:\Users\...\index.js:8:17)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47 {
errno: -4048,
syscall: 'open',
code: 'EPERM',
path: 'C:/Certbot/live/subdomain.example.com/fullchain.pem'
}
The only way I can bypass this is by running as administrator, but this is very strange, as this was not required before.
How can I restore this to its previous behavior?
you just need to change the access rights of the folder and subfolders of : C:/Certbot
add access to the user running nodejs. for example following this :
https://v2cloud.com/tutorials/how-to-change-folder-permissions-on-windows-2016

While Debugging NodeJs getting Error as "Uncaught Error: getaddrinfo ENOTFOUND"

Getting Error as "Uncaught Error: getaddrinfo ENOTFOUND xx.xxx.x.112\ABC" while debugging NodeJs Code in Chrome Debugger.
Below are the Server Details:
Server where NodeJs Code resides: xxx.xx.x.100
Server where SQL is installed: xx.xxx.xx.90
Database Server: xx.xxx.x.112\ABC
Database Server Port: 51269.
Below is my NodeJs Code:
var mysql = require('mysql');
debugger
var con = mysql.createConnection({
host: "xx.xxx.x.112\ABC",
user: "sa",
password: "Pass",
port:51269
});
con.connect(function(err) {
if (err)
throw err;
console.log("Connected!");
});
Below is the Error Details:
VM52 trial2.js:11 Uncaught Error: getaddrinfo ENOTFOUND xx.xxx.x.112\ABC
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:60:26)
--------------------
at Protocol._enqueue (E:\pradeep\Node\SQL_Example\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (E:\pradeep\Node\SQL_Example\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at Connection.connect (E:\pradeep\Node\SQL_Example\node_modules\mysql\lib\Connection.js:116:18)
at Object.<anonymous> (E:\pradeep\Node\SQL_Example\trial2.js:10:5)
at Module._compile (internal/modules/cjs/loader.js:773:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:643:32)
at Function.Module._load (internal/modules/cjs/loader.js:556:12)
at Function.Module.runMain (internal/modules/cjs/loader.js:839:10)
at internal/main/run_main_module.js:17:11
The Error Message is
function dnsException(code, syscall, hostname)
Also check the below screenshot where Error Message is returning from:
Any suggestions will be highly appreciated, as I am new to NodeJs and scratching my head since last 2-days regarding this issue.
Can you try this below code its working for me so.
Or try with removing port and as you already providing the host server.
var mysql = require('mysql');
debugger
var con = mysql.createConnection(config:{
host: "host_for_connection",
user: "user_name",
password: "paaword",
port: your_portNumber
});
con.connect(function(err) {
if (err)
throw err;
console.log("Connected!");
});
Thank you Nilesh for your help, tried the Shared code but getting new Error messsage now as "Uncaught Error: connect ECONNREFUSED 127.0.0.1:3306".
Tried by both with & without Port.
Note: Nodejs code is in other Machine and Sql is installed in another Machine
Below is the NodeJs Code:
var mysql = require('mysql');
debugger
var con = mysql.createConnection({
config:{
host: "xx.xxx.x.112\ABC",
user: "sa",
password: "Pass",
// port:51269
}
});
con.connect(function(err) {
if (err)
throw err;
console.log("Connected!");
});
Below is the Error Message:
VM52 trial2.js:15 Uncaught Error: connect ECONNREFUSED 127.0.0.1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1056:14)
--------------------
at Protocol._enqueue (E:\pradeep\Node\SQL_Example\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (E:\pradeep\Node\SQL_Example\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at Connection.connect (E:\pradeep\Node\SQL_Example\node_modules\mysql\lib\Connection.js:116:18)
at Object.<anonymous> (E:\pradeep\Node\SQL_Example\trial2.js:12:5)
at Module._compile (internal/modules/cjs/loader.js:773:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:643:32)
at Function.Module._load (internal/modules/cjs/loader.js:556:12)
at Function.Module.runMain (internal/modules/cjs/loader.js:839:10)
at internal/main/run_main_module.js:17:11
exceptionWithHostPort # VM9 errors.js:512
afterConnect # VM81 net.js:1056
TickObject (async)
init # VM43 inspector_async_hook.js:21
emitInitNative # VM21 async_hooks.js:134
emitInitScript # VM21 async_hooks.js:341
TickObject # VM22 task_queues.js:102
nextTick # VM22 task_queues.js:130
(anonymous) # VM72 destroy.js:43
Socket._destroy # VM81 net.js:595
destroy # VM72 destroy.js:37
afterConnect # VM81 net.js:1065
TCPCONNECTWRAP (async)
init # VM43 inspector_async_hook.js:21
emitInitNative # VM21 async_hooks.js:134
internalConnect # VM81 net.js:816
defaultTriggerAsyncIdScope # VM21 async_hooks.js:301
emitLookup # VM81 net.js:978
onlookup # VM156 dns.js:65
GETADDRINFOREQWRAP (async)
init # VM43 inspector_async_hook.js:21
emitInitNative # VM21 async_hooks.js:134
lookup # VM156 dns.js:145
(anonymous) # VM81 net.js:958
defaultTriggerAsyncIdScope # VM21 async_hooks.js:301
lookupAndConnect # VM81 net.js:957
Socket.connect # VM81 net.js:891
connect # VM81 net.js:168
connect # VM55 Connection.js:76
(anonymous) # VM52 trial2.js:12
Module._compile # VM44 loader.js:773
Module._extensions..js # VM44 loader.js:787
Module.load # VM44 loader.js:643
Module._load # VM44 loader.js:556
Module.runMain # VM44 loader.js:839
(anonymous) # VM40 run_main_module.js:17

Get permission denied when accessing node.js application azure app services

I have deployed my node.js application on Azure app services I get below error:
{ Error: listen EACCES: permission denied development
at Server.setupListenHandle [as _listen2] (net.js:1262:19)
at listenInCluster (net.js:1327:12)
at Server.listen (net.js:1425:5)
at Function.listen (D:\home\site\wwwroot\node_modules\express\lib\application.js:618:24)
at Server.start (D:\home\site\wwwroot\app.js:33:18)
at new Server (D:\home\site\wwwroot\app.js:30:14)
at Object.<anonymous> (D:\home\site\wwwroot\app.js:101:14)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
code: 'EACCES',
errno: 'EACCES',
syscall: 'listen',
address: 'development',
port: -1 } 'Error: listen EACCES: permission denied development\n at Server.setupListenHandle [as _ listen2] (net.js:1262:19)\n
Try to set your port like below in your app.js or server file where you set the port.
var port = process.env.port || process.env.PORT || 3000;

URL malformed, cannot be parsed

Here is my error captured:
and here are my coding files:
server file
keys file
I just create connection to mlab using mongodb, node and reactjs. I'm using mongo db version 4.
need your help guys.
[nodemon] starting `node server.js`
Error: URL malformed, cannot be parsed
at module.exports (C:\MERNapps\TestMERN08\node_modules\mongodb\lib\url_parser.js:17:21)
at deprecated (internal/util.js:47:15)
at connect (C:\MERNapps\TestMERN08\node_modules\mongodb\lib\operations\mongo_client_ops.js:179:3)
at connectOp (C:\MERNapps\TestMERN08\node_modules\mongodb\lib\operations\mongo_client_ops.js:283:3)
at executeOperation (C:\MERNapps\TestMERN08\node_modules\mongodb\lib\utils.js:420:24)
at MongoClient.connect (C:\MERNapps\TestMERN08\node_modules\mongodb\lib\mongo_client.js:168:10)
at Promise (C:\MERNapps\TestMERN08\node_modules\mongoose\lib\connection.js:493:12)
at new Promise (<anonymous>)
at NativeConnection.Connection.openUri (C:\MERNapps\TestMERN08\node_modules\mongoose\lib\connection.js:490:19)
at Mongoose.connect (C:\MERNapps\TestMERN08\node_modules\mongoose\lib\index.js:230:15)
at Object.<anonymous> (C:\MERNapps\TestMERN08\server.js:12:2)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
(node:3696) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
server running on port 5000
The issue is with your keys. Try using:
module.exports= {
mongoURI : "your-uri-string",
options:{key:value}
}
And use the keys as:
mongoose.connect(db.mongoURI,options);
a very common error with MongoDB atlas. better still follow this procedure
Install Mongo DB compass
use Default setting
hostname: localhost & port 27017
create a database with the project name and then collection: user
Connect with the code below on entry js. route
mongoose.connect('mongodb://localhost/projectname')
.then(() => console.log('mongo db working'))
.catch(err => console.log('ERROR is', err))

Error: connect ETIMEDOUT while connecting to host

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).

Resources