feat: add backend server

This commit is contained in:
2026-05-11 16:01:22 +08:00
parent 3c838d534f
commit e24a34deb4
11 changed files with 1709 additions and 0 deletions

24
server/database.js Normal file
View File

@@ -0,0 +1,24 @@
import mongoose from 'mongoose'
import User from './model/user.js'
const connectDB = async () => {
try {
const conn = await mongoose.connect(process.env.MONGO_URI)
console.log(`MongoDB Connected: ${conn.connection.host}`)
// 检查是否存在管理员用户,如果不存在则创建一个默认管理员
const adminUser = await User.findOne({ username: process.env.ADMIN_USERNAME })
if (!adminUser) {
await User.create({
username: process.env.ADMIN_USERNAME,
nickname: '管理员',
password: process.env.ADMIN_PASSWORD,
role: 'admin',
})
}
} catch (error) {
console.error('Error connecting to MongoDB:', error)
process.exit(1)
}
}
export default connectDB