Skip to content

Commit b8e6b5b

Browse files
committed
rewrite connect db
1 parent 554146e commit b8e6b5b

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed

Database/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
const MongoClient = require("mongodb").MongoClient;
2-
const config = require('../config');
3-
const mongoClient = new MongoClient(`mongodb://${config.database.host}/`);
42

53
class Database {
64
constructor() {
75
this._db = null;
86
}
97

10-
async connect(callback) {
8+
async connect(host, dbname, callback) {
119
try {
10+
const mongoClient = new MongoClient(`mongodb://${host}/`);
11+
1212
await mongoClient.connect();
1313

14-
this._db = mongoClient.db(config.database.dbname);
14+
this._db = mongoClient.db(dbname);
1515

1616
callback();
1717
} catch {

gameserver.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const server = new Server();
88

99
async function run() {
1010
try {
11-
await database.connect(() => {
11+
await database.connect(config.database.host, config.database.dbname, () => {
1212
console.log("database connected: success");
1313
});
1414
} catch(e) {

loginserver.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const server = new Server();
55

66
async function run() {
77
try {
8-
await database.connect(() => {
8+
await database.connect(config.database.host, config.database.dbname, () => {
99
console.log("database connected: success");
1010
});
1111
} catch(e) {

0 commit comments

Comments
 (0)