Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ const { db } = require('./server/db');

const port = process.env.PORT || 3000;

db();
app.listen(port);
console.info(`Listening to http://0.0.0.0:${port}`);
async function start_server() {
await db();
app.listen(port, () => {
console.info(`Listening to http://0.0.0.0:${port}`);
});
}

start_server();
59 changes: 29 additions & 30 deletions server/server/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,35 @@ const { collectionInfo } = require('./type');

async function mongoConnect() { // mongoDB 연결 함수
const secret = await config;
mongoose.connect(secret.mongoURI,
{
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true,
})
.then(async () => {
const collections = await mongoose.connection.db.listCollections().toArray();
let check = collections.findIndex((coll: typeof collectionInfo) => coll.name === 'answerpapers');
if (check === -1) {
await mongoose.connection.db.createCollection('answerpapers');
}
check = collections.findIndex((coll: typeof collectionInfo) => coll.name === 'groups');
if (check === -1) {
await mongoose.connection.db.createCollection('groups');
}
check = collections.findIndex((coll: typeof collectionInfo) => coll.name === 'assignments');
if (check === -1) {
await mongoose.connection.db.createCollection('assignments');
}
check = collections.findIndex((coll: typeof collectionInfo) => coll.name === 'counters');
if (check === -1) {
await mongoose.connection.db.createCollection('counters');
}
console.log('DB와 연결되었습니다.');
})
.catch((e: Error) => {
throw (e);
});
await mongoose.connect(secret.mongoURI, {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이미 then 과 catch로 callback 구조를 이용하고 있는데 await이 같이 존재할 필요가 있을까요?

개인적으로는 깔끔한 코드를 위해 await, try, catch 문만 사용하고 then, catch는 없애버리는게 좋아보입니다.

useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true,
})
.then(async () => {
const collections = await mongoose.connection.db.listCollections().toArray();
let check = collections.findIndex((coll: typeof collectionInfo) => coll.name === 'answerpapers');
if (check === -1) {
await mongoose.connection.db.createCollection('answerpapers');
}
check = collections.findIndex((coll: typeof collectionInfo) => coll.name === 'groups');
if (check === -1) {
await mongoose.connection.db.createCollection('groups');
}
check = collections.findIndex((coll: typeof collectionInfo) => coll.name === 'assignments');
if (check === -1) {
await mongoose.connection.db.createCollection('assignments');
}
check = collections.findIndex((coll: typeof collectionInfo) => coll.name === 'counters');
if (check === -1) {
await mongoose.connection.db.createCollection('counters');
}
console.log('DB와 연결되었습니다.');
})
.catch((e: Error) => {
throw (e);
});
}

exports.db = async () => {
Expand Down