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
๋ฐ์ํ
'๐ Study > JS, JQuery' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
JQuery (0) | 2023.01.14 |
---|---|
[node.js] req.params vs req.body vs req.query (1) | 2022.09.20 |
์์ฒญ ๋ผ์ฐํ ํ๊ธฐ (0) | 2022.05.14 |
๋ฏธ๋ค์จ์ด ์ฌ์ฉํ๊ธฐ (0) | 2022.05.14 |
์ต์คํ๋ ์ค๋ก ์น ์๋ฒ ๋ง๋ค๊ธฐ (0) | 2022.05.14 |