Guest User

Untitled

a guest
Apr 27th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. //set DEBUG=knex* to get more info
  2.  
  3. let activeConnections = 0;
  4.  
  5. const knexSettings = {
  6. client: 'pg',
  7. pool: {
  8. min: 2,
  9. max: 10,
  10. afterCreate: function (conn, done) {
  11. console.log(`New DB Connection! active connections: ${++activeConnections}`);
  12. done(null, conn);
  13. },
  14. beforeDestroy: function (conn, done) {
  15. console.log(`DB Connection Released! active connections: ${--activeConnections}`);
  16. done(null, conn);
  17. },
  18. },
  19. connection: 'postgres://localhost/some_db'//process.env.POSTGRESQLCONNSTR_DB
  20. }
  21.  
  22. const port = 3000; //process.env.PORT
  23.  
  24. const knex = require('knex')(knexSettings);
  25.  
  26. const healthcheck = require('healthcheck-middleware')({
  27. addChecks: function(fail, pass) {
  28. knex.raw('SELECT 1=1;')
  29. .then(function() {
  30. console.log('query was successful');
  31. pass();
  32. })
  33. .catch(function(err) {
  34. console.error('query wasn\'t successful');
  35. console.error(err);
  36. fail(new Error('could not connect to database'));
  37. })
  38. }
  39. });
  40.  
  41. const server = require('express')();
  42.  
  43. server.use('/', healthcheck);
  44.  
  45. server.listen(port);
  46.  
  47. console.log(`Server running on port ${port}`);
Add Comment
Please, Sign In to add comment