๐Ÿ“š Study/JS, JQuery

Mongo DB์™€ Node.js ์—ฐ๋™ํ•˜๊ธฐ

kkh1902 2022. 5. 15. 13:28
728x90
๋ฐ˜์‘ํ˜•

์„œ๋ก 

mongodb์™€ node.js๋ฅผ ์—ฐ๋™ํ•ด์„œ db๋ฅผ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์—ฐ๋™ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„์•ผํ•œ๋‹ค.

 

const mongoose = require('mongoose');
const MONGODB_URL = 'mongodb+srv://root:12341234@cluster0.luvlj.mongodb.net/myFirstDatabase?retryWrites=true&w=majority'

mongoose.connect(MONGODB_URL, { useNewUrlParser: true }, (err) => {
    if (err) {
        console.log(err);
    } else {
        console.log("Connected to database successfully")
    }
})

DB์ ‘๊ทผ์— ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ๋œจ๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒ

  • dotenv๋ผ๋Š” ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•ด์„œ  ๋”ฐ๋กœ ๋งŒ๋“ค์–ด๋‘” variables.env ํด๋”์˜ URL๊ฐ’๋งŒ ๊ฐ€์ ธ์™€์„œ ์‚ฌ์šฉ
const mongoose = require('mongoose');
require('dotenv').config({ path: 'variables.env' });


mongoose.connect(process.env.MONGODB_URL, { useNewUrlParser: true }, (err) => {
    if (err) {
        console.log(err);
    } else {
        console.log("Connected to database successfully")
    }
})

 

728x90
๋ฐ˜์‘ํ˜•