I want to use NodeJS to connect to a SQL Server 2008 R2 Express database.
I've looked around at what NPM has to offer. Have tried mssql, mssql-simple and node-SQLServer.
Some of them can be installed some do not. And those who do, will not access SQL Server.
Error when using mssql:
[Error: connection to .\SQLEXPRESS:1433 - failed Error: getaddrinfo ENOENT]
var sql = require('mssql');
var config = {
user: 'root',
password: 'xxx',
server: '.\SQLEXPRESS',
database: 'test'
}
var connection = new sql.Connection(config, function(err) {
console.log(err);
});
If there is anyone who has got something to work between NodeJS and SQL Server Express? Which module and version do you use?
Problem was that i did not have TCP/IP Enabled.
How to check and Enable MSSQL TCP/IP MSDN
Next you do not have to use "server: '.\SQLEXPRESS'" after enabled TCP/IP 127.0.0.1 works fine!
you could also just put 'localhost' as your server.
// const sql = require("msnodesqlv8");
USE THIS
npm i msnodesqlv8
it is easy to use
Related
I am using the free subscription at Azure and have successfully created a Ubuntu Server and a Flexible Postgres Database.
Until recently I accessed the DB directly from my Windows 10 desktop. Now I want to route all access through the Ubuntu Server.
For this I have installed Open SSH Client and Open SSH Server on my Windows 10 machine and done the necessary local port forwarding with ssh -L 12345:[DB IP]:5432 my_user#[Ubuntu IP]
The connection works, I confirmed it with pgcli on my desktop with pgcli -h 127.0.0.1 -p 12345 -u my_user -d my_db
But when I am trying to connect via node-pg I receive the following error
UnhandledPromiseRejectionWarning: error: no pg_hba.conf entry for host "[Ubuntu IP]", user "my_user", database "my_db", SSL off
I have already added a Firewall Rule in Azure with the [Ubuntu IP], and the error remains. What bugs me further is that in the Azure Portal of the DB I have enabled "Allow public access from any Azure service within Azure to this server", so the extra Firewall should not even be necessary for this connection.
For the last week, I have been stuck on this and now the connection is finally established, but not accessible by my code. Pretty frustrating. I would be glad about ANY pointers on how to fix this.
Edit #1:
I can't post the pg_hba.conf file. Because the Postgres DB is managed by Azure, I do not have access to pg_hba, which makes the situation more difficult to understand.
My node.js code for testing the connection:
const pg = require("pg");
const passwd = "...";
const client = new pg.Client({
user: 'admin',
host: '127.0.0.1',
database: 'test',
password: passwd,
port: 12345
});
client.connect()
client.on('uncaughtException', function (err) {
console.error(err.stack);
});
const query = "SELECT * FROM test";
try {client.query(query, (err,res) => {
if (err) {
console.error(err);
}
console.log(res);
})}
catch (e) {
console.error(e)
}
The comment by #jjanes helped me in understanding the issue, thank you.
This edited pg.Client config solved my problem:
const client = new pg.Client({
user: 'admin',
host: '127.0.0.1',
database: 'test',
password: passwd,
port: 12345,
ssl: {rejectUnauthorized: false}
});
I found this specific SSL option here https://node-postgres.com/features/ssl
In my node web server, I am using a the npm module redis.
when I run my code...
const client = redis.createClient();
client.on("error", function (err) {
console.log("Error " + err);
});
client.hmset(["key", "test keys 1", "test val 1", "test keys 2", "test val 2"], function (err, res) {});
I get an error:
Error Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
I don't have a redis database installed. Do I need that?
If not, anything I have missed in my code?
Yes, you need to install Redis and ensure the server is running. This is a link to the official page for downloading the redis.
In your application code, you need to ensure you are connecting to Redis server with the right port and host address. By default, Redis server should be running on 127.0.0.1:6379 and that is where redis.createClient would try to connect to by default. If your Redis server is running on another port or host, then you need to specify those details when connecting e.g:
redis.createClient({
host: '<the host where redis is running>',
port: '<the port where redis is running>'
});
You can check here for more info on the options you can provide when connecting to Redis server with redis.createClient.
This issue is totally driving me insane. I spent months with this, trying to make a SIMPLE NODE APP WORK. I finally managed to make an APP work in a nice server (Heroku) and with mysql. Problem? The server only accepts postgres. And this is my nightmare. I just cannot make it work. Searched dozens of webs and problems, all of them with the same error log as me... but I just cannot figure what to do. I'm totally idiot at configuring things, I cannot even start programming my app.
Error: connect ECONNREFUSED 127.0.0.1:5432
at Object._errnoException (util.js:1022:11)
at _exceptionWithHostPort (util.js:1044:20)
at TCPConnectWrap.afteeConnect [as oncomplete] (net.js:1198:14)
My start of server.js
const pg = require('pg');
const connectionString = process.env.DATABASE_URL || 'postgres://myrole:12345#localhost:5432/mydb';
And here the error.
var pool = new pg.Pool();
pool.connect().then(client => {
It crashes right at connection.
I did everything I searched for. I created "myrole" login role with all permission, password "12345", to connect to "mydb" database. I opened "pgAdmin4" application. Connected to "PostgreSQL 10" and "mydb". I saw that the first one connects to port 3000. I tried port 3000 in the connection string. I searched for the service at Windows. It's running. I JUST DID EVERYTHING and nothing works... I installed and made MySQL database to run in local in just 2 hours. But Heroku doesn't accept MySQL and I don't want to put any credit card. What's happening here?
I was having the same issue and I'll put my situation here and hopefully help someone else.
I was testing some AWS Lambda functions and since the code runs in a container and the container has its own localhost so my postgres connection was failing because there is no postgres server running on the container. Remember that your machine's localhost is not the same as the container's one, if your app is running inside a container the instead of localhost use your machine IP.
in your case, you shuld include connectionString inside new Pool() as property of Object
var pool = new pg.Pool({connectionString}); pool.connect().then()
or here is a more detailed version
let c = {
host: 'localhost',
port: 5432,
user: 'user',
password: 'password',
database: 'mydb',
options: `application_name=${app}&application_cmd=${cmd}`
};
const connection_string = {connectionString : `postgresql://${c.user}:${c.password}#${c.host}:${c.port}/${c.database}?${c.options}`};
const pool = new Pool(connection_string);
more details can be found here node-postgres.com
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.
Here is my code:
var express = require("express"),
app = express(),
server = require("http").createServer(app),
io = require("socket.io").listen(server),
redis = require("redis"),
env = {PORT: process.env.PORT || 8080, IP: process.env.IP || "localhost"};
client = redis.createClient(env.PORT , env.IP);
client.on("error", function(err) {
console.log(err);
});
server.listen(env.PORT);
console.log("Server started # " + env.IP + ":" + env.PORT);
After trying to run, I received the followings on the console:
Running Node Process
Your code is running at 'http://modified.address.c9.io'.
Important: use 'process.env.PORT' as the port and 'process.env.IP' as the host in your scripts!
info: socket.io started
Server started # modified.ip.address.1:8080
[Error: Auth error: undefined]
I tried establishing the connection, and it connects to the IP and PORT perfectly. However, the error [Error: Auth error: undefined] appears and stops there. I Googled the error, the supports from the IDE I used..., and surprisingly, there are only 7 links to my problems. So I think it may be a hole in my knowledge or it is not really a problem yet a thing I don't know to work it out. All I could pull out from those Google results were (I was not sure) I need to use client.auth(pass) right after creating it. But where should I find the password? When I installed it npm install redis I didn't configure anything and wasn't told to set password whatsoever. So I reach the impasse.
I use Cloud9 IDE (c9.io), and the modules used as shown in the code above.
----With best regards,
----Tim.
I've found out what was wrong.
I did install Redis, but that is a Redis library that acts like a bridge between Redis driver and NodeJS. On Cloud9, I have to manually install Redis, too.
So it would take 2 commands to actually install Redis:
Install the Redis Driver on Cloud9
nada-nix install redis
Install Redis library for NodeJS
npm install redis
Thanks for anyone who was trying to help me.
You can run the redis-server using your own config file.You can create your own config like below.
//port and ip of ur redis server
port 6371
bind 127.0.0.1
//password for this server
requirepass ucanmentionurpwd
//storing snapshots of the data
save 60 1
dbfilename dump.rdb
dir /tmp/db
//starting redis server
redis-server //ur config file location
See this link for redis configuration
https://raw.github.com/antirez/redis/2.6/redis.conf
If you mention requirepass with your password means only you need to do
client.auth('urPwd');
Otherwise no need to call the client.auth method.