index.js 742 B

1234567891011121314151617181920212223242526272829303132
  1. const mongoose = require("mongoose");
  2. require("dotenv").config();
  3. const uriDb = process.env.URI_DB;
  4. const db = mongoose.connect(uriDb, {
  5. useNewUrlParser: true,
  6. useCreateIndex: true,
  7. useUnifiedTopology: true,
  8. useFindAndModify: false,
  9. });
  10. mongoose.connection.on("connected", () =>
  11. console.log("mongoose Database was connect successful to db!")
  12. );
  13. mongoose.connection.on("error", () =>
  14. console.log("mongoose Database was not connect successful to db, error!")
  15. );
  16. mongoose.connection.on("disconnected", () =>
  17. console.log("mongoose Database was disconnected")
  18. );
  19. process.on("SIGINT", async () => {
  20. await mongoose.connection.close();
  21. console.log("Connection for db is closed");
  22. process.exit(1);
  23. });
  24. module.exports = db;