forked from longlife/labplus-server
db: 连接数据库
This commit is contained in:
@@ -12,9 +12,8 @@ module.exports = {
|
|||||||
"NODE_ENV": "production",
|
"NODE_ENV": "production",
|
||||||
"PORT": 80,
|
"PORT": 80,
|
||||||
"DATABASE_URL": "localhost:27017",
|
"DATABASE_URL": "localhost:27017",
|
||||||
"DATABASE_NAME": "labplus",
|
"DATABASE_USER": "labplus_admin",
|
||||||
"DATABASE_USER": "",
|
"DATABASE_PASSWORD": "******",
|
||||||
"DATABASE_PASSWORD": "",
|
|
||||||
}
|
}
|
||||||
}]
|
}]
|
||||||
}
|
}
|
||||||
11
src/index.js
11
src/index.js
@@ -7,9 +7,16 @@ dotenv.config()
|
|||||||
|
|
||||||
const app = Express()
|
const app = Express()
|
||||||
const PORT = process.env.PORT || 8080
|
const PORT = process.env.PORT || 8080
|
||||||
const DATABASE_URL = process.env.DATABASE_URL || "mongodb://localhost:27017/labplus"
|
|
||||||
|
|
||||||
mongoose.connect(DATABASE_URL)
|
// Database Connect String
|
||||||
|
const DB_URL = process.env.DATABASE_URL
|
||||||
|
const DB_USERNAME = process.env.DATABASE_USERNAME
|
||||||
|
const DB_PASSWORD = process.env.DATABASE_PASSWORD
|
||||||
|
const DB_NAME = "labplus"
|
||||||
|
|
||||||
|
const DATABASE_URI = `mongodb://${DB_USERNAME}:${DB_PASSWORD}@${DB_URL}/${DB_NAME}`
|
||||||
|
|
||||||
|
mongoose.connect(DATABASE_URI)
|
||||||
const db = mongoose.connection
|
const db = mongoose.connection
|
||||||
db.on("error", (error) => console.error(error))
|
db.on("error", (error) => console.error(error))
|
||||||
db.once("open", () => console.log("Connected to Database"))
|
db.once("open", () => console.log("Connected to Database"))
|
||||||
|
|||||||
Reference in New Issue
Block a user