链接数据库

This commit is contained in:
2024-08-11 21:47:44 +08:00
parent 2f510aa096
commit ad0a91150e
5 changed files with 608 additions and 188 deletions

783
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -13,8 +13,8 @@
"author": "hbk01",
"license": "GPL-3.0-or-later",
"dependencies": {
"dotenv": "^16.3.1",
"express": "^4.18.2",
"mongoose": "^8.0.3"
"mongoose": "^8.0.3",
"nodemon": "^3.1.0"
}
}

View File

@@ -11,7 +11,8 @@ module.exports = {
env: {
"NODE_ENV": "production",
"PORT": 80,
"DATABASE_URI": "mongodb://USERNAME:PASSWORD@localhost:27017",
// TODO: change to your own username and password
"DATABASE_URI": "mongodb://USERNAME:PASSWORD@localhost:27017/labplus",
}
}]
}

View File

@@ -33,7 +33,8 @@ router.put("/:id", async (req, res) => {
router.delete("/:id", async (req, res) => {
const user = await User.findByIdAndDelete(req.params.id).then(() => {
res.json({
"message": "User deleted successfully"
"message": "deleted successfully",
user
})
}).catch((err) => {
res.json({ error: err })

View File

@@ -1,17 +1,14 @@
import Express from "express"
import dotenv from "dotenv"
import router from "./router.js"
import mongoose from "mongoose"
dotenv.config()
const app = Express()
const PORT = process.env.PORT || 8080
mongoose.connect(process.env.DATABASE_URI)
const db = mongoose.connection
db.on("error", (error) => console.error(error))
db.on("error", console.error.bind(console, "connection error:"))
db.once("open", () => console.log("Connected to Database"))
app.use(Express.json())