first commit
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
import express from 'express';
|
||||
import cors from 'cors';
|
||||
import authRouter from './routes/auth.js';
|
||||
import postsRouter from './routes/posts.js';
|
||||
import noticesRouter from './routes/notices.js';
|
||||
import secondhandRouter from './routes/secondhand.js';
|
||||
import housekeepingRouter from './routes/housekeeping.js';
|
||||
import db from './db.js';
|
||||
import bcrypt from 'bcryptjs';
|
||||
|
||||
const app = express();
|
||||
app.use(cors());
|
||||
app.use(express.json({ limit: '10mb' }));
|
||||
|
||||
// 初始化管理员账号
|
||||
function seedAdmin() {
|
||||
const admin = db.prepare('SELECT id FROM users WHERE username = ?').get('admin');
|
||||
if (!admin) {
|
||||
const hash = bcrypt.hashSync('admin123', 10);
|
||||
db.prepare('INSERT INTO users (username, password, nickname, role, building) VALUES (?, ?, ?, ?, ?)')
|
||||
.run('admin', hash, '社区管理员', 'admin', '物业中心');
|
||||
console.log('已创建管理员账号: admin / admin123');
|
||||
}
|
||||
// 初始化示例通知
|
||||
const count = db.prepare('SELECT COUNT(*) as c FROM notices').get().c;
|
||||
if (count === 0) {
|
||||
db.prepare('INSERT INTO notices (title, content, type) VALUES (?, ?, ?)')
|
||||
.run('停水通知', '因管道维修,本周六 8:00-12:00 小区将停水,请各位业主提前储水。', '通知');
|
||||
db.prepare('INSERT INTO notices (title, content, type) VALUES (?, ?, ?)')
|
||||
.run('关于规范电动车停放的通知', '请将电动车停放在指定充电棚,严禁上楼充电,感谢配合。', '通知');
|
||||
}
|
||||
}
|
||||
seedAdmin();
|
||||
|
||||
app.use('/api/auth', authRouter);
|
||||
app.use('/api/posts', postsRouter);
|
||||
app.use('/api/notices', noticesRouter);
|
||||
app.use('/api/secondhand', secondhandRouter);
|
||||
app.use('/api/housekeeping', housekeepingRouter);
|
||||
|
||||
// 静态文件(前端构建产物)
|
||||
import path from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
app.use(express.static(path.join(__dirname, '../client/dist')));
|
||||
|
||||
app.use((err, req, res, next) => {
|
||||
console.error(err);
|
||||
res.status(500).json({ message: '服务器内部错误' });
|
||||
});
|
||||
|
||||
const PORT = process.env.PORT || 3000;
|
||||
app.listen(PORT, () => {
|
||||
console.log(`生活圈系统后端已启动: http://localhost:${PORT}`);
|
||||
});
|
||||
Reference in New Issue
Block a user