Why I can't send an api request to database on heroku? - node.js

I have an app built with react, redux, node, and postgresql(knex), and I could deploy this app to heroku. However, all the api requests to database made in an app does not work. It is not fetching any data or I can not sign up.
In the app, I get a console error like this:
Failed to load resource: the server responded with a status of 503 (Service Unavailable)
Uncaught (in promise) Error: Request failed with status code 503(…)
...
Is there a way to fix this?
My knex configuration:
module.exports = {
development: {
client: 'postgresql',
connection: {
database: 'database5',
user: 'user',
password: ''
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'knex_migrations'
}
},
staging: {
client: 'postgresql',
connection: {
database: 'my_db',
user: 'username',
password: 'password'
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'knex_migrations'
}
},
production: {
client: 'postgresql',
connection: process.env.DATABASE_URL,
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'knex_migrations'
}
}
};
Error in heroku logs:
2016-10-25T13:06:04.647342+00:00 app[web.1]: Unhandled rejection Error: connect ECONNREFUSED 127.0.0.1:5432
2016-10-25T13:06:04.647361+00:00 app[web.1]: at Object.exports._errnoException (util.js:893:11)
2016-10-25T13:06:04.647362+00:00 app[web.1]: at exports._exceptionWithHostPort (util.js:916:20)

Related

mssql connection using strapi

A strapi API app connects and works perfectly on SQLlite. however we need it to connect to SQL Express db. the msssql module is installed through npm i mssql and the connection strings have been amended in order to connect to mssql heres the config file
/*
const fs = require( "fs" );
module.exports = ( { env } ) => ( {
defaultConnection: 'default',
connections: {
default: {
connector: 'bookshelf',
settings: {
client: 'sqlite',
filename: env( 'DATABASE_FILENAME', '.tmp/data.db' ),
},
options: {
useNullAsDefault: true,
},
},
},
} );
*/
const sql = require('mssql')
module.exports = ( { env } ) => ( {
defaultConnection: 'default',
connections: {
default: {
connector: 'mssql',
settings: {
user: "***",//process.env.DB_USER,
password: "***",//process.env.DB_PWD,
database: "***",//process.env.DB_NAME,
server: "**.**.**.**",
port: 1433,
connectionTimeOut: 150000,
pool: {
max: 10,
min: 0,
idleTimeoutMillis: 30000
},
options: {
enableArithAbort: true,
encrypt: false,
trustedConnection: true
},
useNullAsDefault: true
}
}
}
})
the command lines commended out (for the code above) is the previous connection to sql lite that works.
now the error message is the following:
PS C:*Team\Q*_Team\AngularAPI\ui\molla-angular\strapi> npm start
mollastrapi#0.1.0 start
strapi start
[2022-07-29T08:05:53.028Z] debug ⛔️ Server wasn't able to start properly.
[2022-07-29T08:05:53.029Z] error TypeError: requireConnector(...) is not a function
at Object.load (C:\****Team\Q****_Team\AngularAPI\ui\molla-
angular\strapi\node_modules\strapi-database\lib\connector-registry.js:20:65)
at DatabaseManager.initialize (C:\****Team\Q****_Team\AngularAPI\ui\molla-
angular\strapi\node_modules\strapi-database\lib\database-manager.js:32:21)
at Strapi.load (C:\****Team\Q****_Team\AngularAPI\ui\molla-
angular\strapi\node_modules\strapi\lib\Strapi.js:297:19)
at async Strapi.start (C:\****Team\Q***_Team\AngularAPI\ui\molla-
angular\strapi\node_modules\strapi\lib\Strapi.js:156:9)
PS C:\****Team\Q****_Team\AngularAPI\ui\molla-angular\strapi>

Node-mssql not able to connect to the server but with tedious it connects

currently i'am using tedious package to connect to the database and do operations but i would like to switch to node-mssql (seems less messy).
The problem i'm getting is connection timeout:
originalError: ConnectionError: Failed to connect to yyy:1433 in 15000ms
code: 'ETIMEOUT',
isTransient: undefined
}
My config with tedious :
const config = {
server: process.env.HOST, // update me
authentication: {
type: 'default',
options: {
userName: process.env.USER, // update me
password: process.env.PASS, // update me
},
},
options: {
// If you are on Microsoft Azure, you need encryption:
database: process.env.DB,
rowCollectionOnDone: true, // update me
},
};
My config with mssql :
const configMssql = {
user: process.env.USER,
password: process.env.PASS,
server: process.env.HOST, // update me
database: process.env.DB,
pool: {
max: 10,
min: 0,
idleTimeoutMillis: 30000,
},
options: {
encrypt: false, // for azure
trustServerCertificate: false, // change to true for local dev / self-signed certs
},
};
or
const configMssqlString = `Server=${process.env.HOST},1433;Database=${process.env.DB};User Id=${process.env.USER};Password=${process.env.PASS};Encrypt=false`;
Can't figure out whats wrong

cannot migrate my knex migration table to postgres on heroku

I'm following a tutorial on youtube that uses node express knex and sqlite locally and postgres on heroku. I managed to get everything working locally with sqlite and have managed to load the app on heroku. I get the initial home message. I manged to create a blank postgres database on heroku and can look at the database credentials on heroku. I need to migrate my knex tables to the heroku postgres database. According to the video below I need to use this instruction
heroku run knex migrate:latest -a node-knex1
Which gives me the following error
Running knex migrate:latest on ⬢ node-knex1... up, run.3492 (Free)
Using environment: production
error: no pg_hba.conf entry for host "54.76.162.141", user "vnujkqszmxsboi", database "def52ulvb1tjg9", SSL off
at Parser.parseErrorMessage (/app/node_modules/pg-protocol/dist/parser.js:278:15)
at Parser.handlePacket (/app/node_modules/pg-protocol/dist/parser.js:126:29)
at Parser.parse (/app/node_modules/pg-protocol/dist/parser.js:39:38)
at Socket.<anonymous> (/app/node_modules/pg-protocol/dist/index.js:10:42)
at Socket.emit (events.js:315:20)
at addChunk (internal/streams/readable.js:309:12)
at readableAddChunk (internal/streams/readable.js:284:9)
at Socket.Readable.push (internal/streams/readable.js:223:10)
at TCP.onStreamRead (internal/stream_base_commons.js:188:23)
my knexfile.js looks like the following.
module.exports = {
development: {
client: 'sqlite3',
connection: {
filename: './data/lessons.db3',
},
useNullAsDefault: true,
pool: {
afterCreate: (conn, done) => {
conn.run('PRAGMA foreign_keys = ON', done);
},
},
},
production: {
client: 'pg',
connection: process.env.DATABASE_URL,
pool: {
min: 2,
max: 10,
},
migrations: {
tablename: 'knex-migrations',
directory: './migrations',
},
},
};
[Node Express Tutorial 18 - Setting up a Postgres database in
Heroku][1]
[1]: https://www.youtube.com/watch?v=OZQWfW3VvhE&list=PLKii3VqdFnoZY6EBxb2K37D0wrEmS-5RD&index=17
I found I could overcome my problem by changing the knexfile.js to something close to what is in the heroku documentation.
module.exports = {
development: {
client: 'sqlite3',
connection: {
filename: './data/lessons.db3',
},
useNullAsDefault: true,
pool: {
afterCreate: (conn, done) => {
conn.run('PRAGMA foreign_keys = ON', done);
},
},
},
production: {
client: 'pg',
connection: {
connectionString: process.env.DATABASE_URL,
ssl: { rejectUnauthorized: false },
},
migrations: {
directory: __dirname + '/migrations',
},
seeds: {
directory: __dirname + '/seeds',
},
},
};

Heroku Postgres Typeorm Migrations

I am trying to perform my migrations on the database provided by Heroku, but the following error appears:
Error during migration run:
Error: self signed certificate
at TLSSocket.onConnectSecure (_tls_wrap.js:1501:34)
at TLSSocket.emit (events.js:315:20)
at TLSSocket._finishInit (_tls_wrap.js:936:8)
at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:710:12) {
code: 'DEPTH_ZERO_SELF_SIGNED_CERT'
}
ormconfig.js
module.exports = {
type: `${process.env.DATABASE_TYPE}`,
host: `${process.env.DATABASE_HOST}`,
port: `${process.env.DATABASE_PORT}`,
username: `${process.env.DATABASE_USERNAME}`,
password: `${process.env.DATABASE_PASSWORD}`,
database: `${process.env.DATABASE_NAME}`,
entities: [`${process.env.DATABASE_ENTITIES}`],
migrations: [`${process.env.DATABASE_MIGRATIONS}`],
cli: { migrationsDir: `${process.env.DATABASE_MIGRATIONS_DIR}` },
extra: {
ssl: true
}
};
module.exports = {
type: `postgres`,
url: `${process.env.DATABASE_URL}`,
entities: [`${process.env.DATABASE_ENTITIES}`],
migrations: [`${process.env.DATABASE_MIGRATIONS}`],
cli: { migrationsDir: `${process.env.DATABASE_MIGRATIONS_DIR}` },
extra: {
ssl: {
rejectUnauthorized: false,
}
}
};
Works fine!

Connecting to (LocalDB)\MSSQLLocalDB Sequelize

I have a question about sequelize and sql server.
So I can connect to my database with "localhost" or name of my computer, but i cant to "(LocalDB)\MSSQLLocalDB". This is my connection parameter.
PASSWORD: "sw",
DB: "BusinessDB",
CONFIG: {
host: '(LocalDB)\\MSSQLLocalDB',
dialect: 'mssql',
dialectOptions: {
options: {
encrypt: true,
}
},
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
},
define:{
timestamps: false,
}
}
```
And this is the error when i'm trying to connect with this config
> Failed to connect to (LocalDB)\MSSQLLocalDB:1433 - getaddrinfo ENOTFOUND (LocalDB)\MSSQLLocalDB
Someone have the solutions for that. I search on google but I doesn't find a solution.
Thanx
I found a solution with the help of "msnodesqlv8" module.
Now i'm using this configuration for connect to my DB.
dialect: 'mssql',
dialectModule: require('msnodesqlv8/lib/sequelize'),
bindParam: false,
/*logging: false,*/
dialectOptions: {
options: {
connectionString: 'Driver={ODBC Driver 17 for SQL Server};Server= (LocalDB)\\MSSQLLocalDB;Database=MyDB;Trusted_Connection=yes;',
},
},
define:{
timestamps: false,
}
The driver version can be found on ODBC data sources software.(type in windows search bar)

Resources