Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

mongoose connection in express

//config/db.js
const mongoose = require('mongoose');

const connect = async () => {
  try {
    await mongoose.connect(process.env.MONGODB);
    console.log('database connect');
  } catch (error) {
    console.log(error.message);
  }
};

module.exports = connect;

//index.js 
require('dotenv').config();
const connect = require('./config/db');

connect();

Source by mongoosejs.com #
 
PREVIOUS NEXT
Tagged: #mongoose #connection #express
ADD COMMENT
Topic
Name
3+1 =