Advertisement
Guest User

Untitled

a guest
Aug 31st, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. var mongoose = require('mongoose');
  2. var config = require('./config/database.js');
  3. var DB_URL = config.db.url;
  4.  
  5. mongoose.connection.on("connected", function(ref) {
  6. console.log("Connected to " + " DB!");
  7. });
  8.  
  9. mongoose.connection.on("error", function(err) {
  10. console.error('Failed to connect to DB ' + ' on startup ', err);
  11. if (err) {
  12. return next(err);
  13. }
  14. });
  15.  
  16. mongoose.connection.on('disconnected', function(err) {
  17. console.log('Mongoose default connection to DB :' + ' disconnected');
  18. if (err) {
  19. return next(err);
  20. }
  21. });
  22.  
  23. var gracefulExit = function() {
  24. mongoose.connection.close(function () {
  25. console.log('Mongoose default connection with DB :' + ' is disconnected through app termination');
  26. process.exit(0);
  27. });
  28. }
  29.  
  30. process.on('SIGINT', gracefulExit).on('SIGTERM', gracefulExit);
  31.  
  32. exports.con_close = function () {
  33. console.log('Mongoose connection disconnected');
  34. mongoose.connection.close();
  35. }
  36.  
  37. var options = {
  38. server: {
  39. socketOptions: {
  40. keepAlive: 1000,
  41. connectTimeoutMS: 30000
  42. }
  43. },
  44. replset: {
  45. rs_name: 'replicaset',
  46. auto_reconnect:true,
  47. socketOptions: {
  48. keepAlive: 1000, // doubt about it
  49. connectTimeoutMS: 30000
  50. }
  51. },
  52. user: 'root',
  53. pass: 'G3saGT2Y',
  54. auth: {
  55. authdb: 'admin'
  56. }
  57. }
  58.  
  59. mongoose.connect(DB_URL, options, function(err) {
  60. console.log('ho rha hai');
  61. if (err) {
  62. console.log('error connection to mongo server!');
  63. console.log(err);
  64. }
  65. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement