Page 532 - Nodejs 교과서 개정2판
P. 532
schemas/index.js
const mongoose = require('mongoose');
const { MONGO_ID, MONGO_PASSWORD, NODE_ENV } = process.env;
const MONGO_URL = `mongodb://${MONGO_ID}:${MONGO_PASSWORD}@localhost:27017/admin`;
const connect = () => {
if (NODE_ENV !== 'production') {
mongoose.set('debug', true);
}
mongoose.connect(MONGO_URL, {
dbName: 'gifchat',
useNewUrlParser: true,
useCreateIndex: true,
}, (error) => {
if (error) {
console.log('몽고디비 연결 에러', error);
} else {
console.log('몽고디비 연결 성공');
}
});
};
mongoose.connection.on('error', (error) => {
console.error('몽고디비 연결 에러', error);
});
mongoose.connection.on('disconnected', () => {
console.error('몽고디비 연결이 끊겼습니다. 연결을 재시도합니다.');
connect();
});
module.exports = connect;
.env
COOKIE_SECRET=gifchat
MONGO_ID=root
MONGO_PASSWORD=nodejsbook
٘৬ ѱ ܰ ঋणפ ݅ ࠁউਸ ਤ೧ ই٣৬ ࠺ߣഐܳ process.env۽ ܻ࠙೮णפ
ژೠ ؘఠ߬झ ܴ HJGDIBUਵ۽ ߄Շणפ
ࢲߡܳ प೯ೡ ٸ ކҊ٣࠺ী ߄۽ ࣘೡ ࣻ ب۾ ࢲߡ৬ ކҳझܳ োѾפ