Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //set DEBUG=knex* to get more info
- let activeConnections = 0;
- const knexSettings = {
- client: 'pg',
- pool: {
- min: 2,
- max: 10,
- afterCreate: function (conn, done) {
- console.log(`New DB Connection! active connections: ${++activeConnections}`);
- done(null, conn);
- },
- beforeDestroy: function (conn, done) {
- console.log(`DB Connection Released! active connections: ${--activeConnections}`);
- done(null, conn);
- },
- },
- connection: 'postgres://localhost/some_db'//process.env.POSTGRESQLCONNSTR_DB
- }
- const port = 3000; //process.env.PORT
- const knex = require('knex')(knexSettings);
- const healthcheck = require('healthcheck-middleware')({
- addChecks: function(fail, pass) {
- knex.raw('SELECT 1=1;')
- .then(function() {
- console.log('query was successful');
- pass();
- })
- .catch(function(err) {
- console.error('query wasn\'t successful');
- console.error(err);
- fail(new Error('could not connect to database'));
- })
- }
- });
- const server = require('express')();
- server.use('/', healthcheck);
- server.listen(port);
- console.log(`Server running on port ${port}`);
Add Comment
Please, Sign In to add comment