From 2f510aa09643512a3f77611f3c6f23b4c48fd27f Mon Sep 17 00:00:00 2001 From: hbk01 <3243430237@qq.com> Date: Sat, 17 Feb 2024 19:47:52 +0800 Subject: [PATCH] connect db --- index.html | 20 ++++++++++++++++++++ pm2.config.cjs | 4 +--- src/index.js | 11 ++--------- 3 files changed, 23 insertions(+), 12 deletions(-) create mode 100644 index.html diff --git a/index.html b/index.html new file mode 100644 index 0000000..a420124 --- /dev/null +++ b/index.html @@ -0,0 +1,20 @@ + + + + + + Document + + + + +
+ + \ No newline at end of file diff --git a/pm2.config.cjs b/pm2.config.cjs index bfb80dd..8d7d9ab 100644 --- a/pm2.config.cjs +++ b/pm2.config.cjs @@ -11,9 +11,7 @@ module.exports = { env: { "NODE_ENV": "production", "PORT": 80, - "DATABASE_URL": "localhost:27017", - "DATABASE_USER": "labplus_admin", - "DATABASE_PASSWORD": "******", + "DATABASE_URI": "mongodb://USERNAME:PASSWORD@localhost:27017", } }] } \ No newline at end of file diff --git a/src/index.js b/src/index.js index 31e2285..cad62a2 100644 --- a/src/index.js +++ b/src/index.js @@ -8,16 +8,9 @@ dotenv.config() const app = Express() const PORT = process.env.PORT || 8080 -// 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) +mongoose.connect(process.env.DATABASE_URI) const db = mongoose.connection + db.on("error", (error) => console.error(error)) db.once("open", () => console.log("Connected to Database"))