NodeJs Server on production - javascript

So i made a simply express server connected with a database. On localhost it connects with database perfectly but on deployment it shows an error like that
{"code":"ESOCKET","originalError":{"code":"ESOCKET"},"name":"ConnectionError"}
On sql Server everything is set perfectly.
my configuration looks like that:
const config={
user: 'xxxxx',
password: 'xxxxxxx',
server: 'public_ip_from_sql',
database: 'data_base',
debug:true,
port: 1433,
client:"mssql",
options: {
encrypt: false,
enableArithAbord:true,
trustServerCertificate: true,
validateBulkLoadParameters:true,
},
connectionTimeout:150000,
pool:{
max:10,
min:0,
idleTimeoutMillis:3000
}
}
I am hosting it on Plesk nginx server. i havent find any solution to solve this problem.

Related

Knex Heroku Error: self signed certificate

I keep getting this error:
Error: self signed certificate
When running this command in the terminal:
knex migrate:latest --env production
My knexfile.js
require('dotenv').config();
module.exports = {
development: {
client: "pg",
connection: {
host: "localhost",
database: "my-movies"
}
},
production: {
client: "pg",
connection: process.env.DATABASE_URL
}
};
My .env file:
DATABASE_URL=<my_database_url>?ssl=true
Heroku app info:
Addons: heroku-postgresql:hobby-dev
Auto Cert Mgmt: false
Dynos:
Git URL: https://git.heroku.com/path-name.git
Owner: xxxxxxxxx#xxxx.com
Region: us
Repo Size: 0 B
Slug Size: 0 B
Stack: heroku-18
Web URL: https://my-appname.herokuapp.com/
I've tried putting a key value pair in the production in the knexfile of ssl: true and I get the same error. I've done it this way in the past many, many times and have never had this issue. Wondering if Heroku has changed anything but while searching their docs I couldn't find anything.
The following config at knexfile.js worked for me.
...
production: {
client: 'postgresql',
connection: {
connectionString: process.env.DATABASE_URL,
ssl: { rejectUnauthorized: false }
}
}
...
where the DATABASE_URL is what you get by running heroku config --yourAppName
This is due to a breaking change in pg#^8 (2020/02/25) cf. this heroku help forum.
You can get the full pg#^8 announcement but here is the relevant passage:
Now we will use the default ssl options to tls.connect which includes rejectUnauthorized being enabled. This means your connection attempt may fail if you are using a self-signed cert.
And it seems heroku is using self-signed certificates somewhere.
possible solutions:
downgrade to pg#^7
instruct pg#^8 to ignore problematic certificates ssl: { rejectUnauthorized: false } (see announcement linked above)
find a way to download and trust the certificate instructions
The ssl: { rejectUnauthorized: false } pg config isn't working for me at the moment either.. but I found a temporary (maybe permanent) solution via the heroku docs
Set the following config var:
heroku config:set PGSSLMODE=no-verify
If you are using a config like:
...
production: {
client: 'postgresql',
connection: {
connectionString: process.env.DATABASE_URL,
ssl: { rejectUnauthorized: false }
}
}
...
...and it still isn't working for you, make sure you don't have a ?ssl=true or sslmode set in DB your connection string.
If ssl is set in your connection string it will override the ssl part of your config, meaning behavior is equivalent to:
...
production: {
client: 'postgresql',
connection: {
connectionString: process.env.DATABASE_URL,
ssl: true
}
}
...
Removing the ssl entry from your connection string will fix the problem.
What worked for me was not using just a connection string but also adding the CA from my database as an option to the connection object in knex.
production: {
client: 'postgresql',
connection: {
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false,
ca: process.env.POSTGRES_CA,
}
}
}

Why is Sequelize not authenticating against my MS Sql Database?

When attempting to connect to my local SQL Server instance I am receiving an error stating Authentication failed for login. However I am able to login directly to the server in SQL using the provided login.
Here is my code that is attempting to connect to the server.
var Sequelize = require('sequelize');
const sequelize = new Sequelize('GraphQLTests', 'gql', 'Password1', {
dialect: 'mssql',
host:'localhost'
});
sequelize
.authenticate()
.then(() => {
console.log('Connection has been established successfully.');
})
.catch(err => {
console.error('Unable to connect to the database:', err);
});
I have printed to the console in the Sequelize code to verify that the correct credentials are getting passed but still receive this error.
name: 'SequelizeAccessDeniedError',
parent:
{ ConnectionError: Login failed for user ''.}
Please let me know if there is any other info I can provide.
try this
const sequelize = new Sequelize('DB Name', 'Username', 'Password', {
host: 'Host',
dialect: 'mssql',
dialectOptions: {
options: {
encrypt: true,
}
}
});
sequelize.authenticate().then((err) => {
console.log('Connection successful', err);
})
.catch((err) => {
console.log('Unable to connect to database', err);
});
Try this link as reference Connecting to MSSQL server with Sequelize
This is what worked for me, where I have put the server details in an YAML file. (this way, you can switch servers on the fly).
this is the sequelize code
//get the configure from the YAML file
const YAML = await fs.readFile(process.env.SEQUELIZE_CONNECT,'utf8');
//load the database parameters into our system
const params = jsyaml.safeLoad(YAML, 'utf8');
//initiate our database server details to connect to our underlying database system
//as described in the YAML file.
sequlz = new Sequelize(params.dbname, params.username, params.password, params.params);
Here is how my YAML file looks. (I have left my code comments as it is)
#this should work to whatever you are using anywhere.
#as per the YAML file title, I am using a MS SQL server hosted on Azure.
# you can edit values. as per your requirement.
# check the dialect help file of your server on the sequelize documentation
# https://sequelize.org/v5/file/lib/dialects/mssql/connection-manager.js.html
#change the values as per your server.
dbname: databasenamehere
username: usernamehere
password: passwordhere
params:
host: servernamehere.database.windows.net
dialect: mssql
dialectOptions:
{
options: {
encrypt: true,
requestTimeout: 10000
}
}
So, that worked for me. (I have remixed answers from above post, and also from the textbook and multiple online resources I was referring).
Note : The server is running on Azure with the Firewall IP address set to ALL IP address.

Node.js knex - Securing the password used for logging into database

I have the following code in a file called knexfile.js
module.exports = {
development: {
client: 'mysql',
connection: {
database: 'myDatabase',
timezone: 'Z',
user: 'root',
password: 'myPassword',
host: '127.0.0.1'
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'myMigrationTable'
}
}
};
myPassword from the code above is in plaintext. On my production server, I definitely don't want my password in plaintext in my code that my application uses to authenticate with my database. I also wouldn't want it laying around in a file in plaintext on my server.
Is there a way in knex or node to easily handle securely logging into my database? Should I just simply encrypt my password, leave it in a file on my server, and decrypt it using my webapp when it's going to log in?
Best practice would be using environment variable.
knex = require('knex')({
client: 'mysql',
connection: process.env.DATABASE_URL
})

Sails.js MongoDB in bluemix not working

I have a node.js app using the sails.js framework and I'm trying to deploy this app on the bluemix cloud service.
I am trying a MongoDB instance in compose.io and I have a rather standard connection configuration in my local.js file:
connectMongo: {
adapter: 'sails-mongo',
host: 'sl-eu-lon-2-portal.1.dblayer.com',
port: 10438,
database: 'some-db'
}
It is not working. It's not deploying.
The error it gives is:
ERR error: A hook (`orm`) failed to load!
ERR error: Error: Failed to connect to MongoDB.
This means, of course, that the database is
But strangely it also gives this
ERR { [MongoError: connect ECONNREFUSED 127.0.0.1:27017]
Which doesn't make any sense, as I am not using port 27017, as noted above I am using 10438.
The app is running locally, so I get that I am missing something on connecting to the database via the bluemix configurations, but I can't understand how come the 27017 pops up there.
So your setup seems correct. Compare it with my setup:
env/production.js
connections: {
prodMongoDb: {
adapter: 'sails-mongo',
host: process.env.MONGO_PORT_27017_TCP_ADDR,
port: 27017,
database: 'my_database'
}
},
models: {
connection: 'prodMongoDb',
migrate: 'safe'
}
env/development.js
connections: {
devMongoDb: {
adapter: 'sails-mongo',
host: 'localhost',
port: 27017,
database: 'my_database'
}
},
models: {
connection: 'devMongoDb',
migrate: 'safe'
}
The fact that you are specifying a port 10438 but get an error regarding 27017 means that sails is not picking up your connection definition. How do you start your app?
Starting it like this:
npm start NODE_ENV="production"
will make sails pick up the production config.

Connect app to RethinkDB server on AWS

I am attempting to connect my app, which is currently on localhost, to a rethinkdb server on AWS. I used the rethinkdb AMI to get the server configured and up and running. However, I keep getting a failed to connect message. I am using rethinkdbdash to attempt the connection (https://github.com/neumino/rethinkdbdash). The following is my connection code and no passsword on the db for right now. Anyone know how to connect to it:
let r = rethinkdbdash({
db: 'test',
user: 'rethinkdb',
servers: [{host: 'my.aws.ip.addr', port: '28015'}]
});
Made a mistake in the connection syntax... the actual syntax should have been:
let r = rethinkdbdash(
{
host: 'ip.addr.here',
db: 'test',
});

Categories