Node.js基于Vue框架中药房管理平台
以下是一个简化版的Node.js后端代码示例,用于处理药房管理平台的药房信息:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const port = 3000;
// 中间件设置
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// 药房信息数组模拟数据库
let pharmacies = [
{ id: 1, name: '药店1', address: '地址1' },
{ id: 2, name: '药店2', address: '地址2' }
];
// 获取所有药房信息
app.get('/pharmacies', (req, res) => {
res.send(pharmacies);
});
// 创建新药房
app.post('/pharmacies', (req, res) => {
const newPharmacy = { id: pharmacies.length + 1, ...req.body };
pharmacies.push(newPharmacy);
res.status(201).send(newPharmacy);
});
// 更新药房信息
app.put('/pharmacies/:id', (req, res) => {
const found = pharmacies.find(p => p.id === parseInt(req.params.id));
if (found) {
const index = pharmacies.indexOf(found);
pharmacies[index] = { ...found, ...req.body };
res.send(pharmacies[index]);
} else {
res.status(404).send('Drugstore not found');
}
});
// 删除药房信息
app.delete('/pharmacies/:id', (req, res) => {
const found = pharmacies.find(p => p.id === parseInt(req.params.id));
if (found) {
pharmacies = pharmacies.filter(p => p.id !== parseInt(req.params.id));
res.status(204).send('Drugstore deleted');
} else {
res.status(404).send('Drugstore not found');
}
});
// 启动服务器
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});
这段代码提供了创建、读取、更新和删除药房信息的基础功能。在实际应用中,您可能需要与数据库集成,并添加更复杂的业务逻辑。
评论已关闭