Advertisement
Guest User

Untitled

a guest
Jul 25th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. const express = require("express");
  2.  
  3. const databaseConfig= {
  4. "host": "localhost",
  5. "port": 5432,
  6. "database": "library_app",
  7. "user": "postgres"
  8. };
  9.  
  10. const pgp = require("pg-promise")({});
  11. const db = pgp(databaseConfig);
  12.  
  13. const app = express();
  14. const port = 5000;
  15.  
  16. app.listen(port, (err) => {
  17. console.log(`running server on port: ${port}`);
  18. });
  19.  
  20. const db = pgp(connection);
  21.  
  22. const initOptions = {
  23. // global event notification;
  24. error: (error, e) => {
  25. if (e.cn) {
  26. // A connection-related error;
  27. //
  28. // Connections are reported back with the password hashed,
  29. // for safe errors logging, without exposing passwords.
  30. console.log('CN:', e.cn);
  31. console.log('EVENT:', error.message || error);
  32. }
  33. }
  34. };
  35.  
  36. const pgp = require('pg-promise')(initOptions);
  37.  
  38. // using an invalid connection string:
  39. const db = pgp('postgresql://userName:password@host:port/database');
  40.  
  41. db.connect()
  42. .then(obj => {
  43. obj.done(); // success, release the connection;
  44. })
  45. .catch(error => {
  46. console.log('ERROR:', error.message || error);
  47. });
  48.  
  49. db.proc('version')
  50. .then(data => {
  51. // SUCCESS
  52. // data.version =
  53. // 'PostgreSQL 9.5.1, compiled by Visual C++ build 1800, 64-bit'
  54. })
  55. .catch(error => {
  56. // connection-related error
  57. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement