给出分类管理后端的接口,包含列表、新增、修改、删除、查看,我的后端是nodejs,express,使用mysql,给出代码,将后端接口单独封装在分类模块里
时间: 2024-04-15 12:30:01 浏览: 38
下面是一个基于 Node.js、Express 和 MySQL 的分类管理后端接口的示例代码:
首先,创建一个名为 `categories.js` 的分类模块文件,并在其中定义相应的接口:
```javascript
// categories.js
const express = require('express');
const router = express.Router();
const mysql = require('mysql');
// 创建 MySQL 连接池
const pool = mysql.createPool({
host: 'localhost',
user: 'your_username',
password: 'your_password',
database: 'your_database'
});
// 获取分类列表接口
router.get('/', (req, res) => {
pool.query('SELECT * FROM categories', (error, results) => {
if (error) {
console.error('Error retrieving categories: ', error);
res.status(500).json({ error: 'Failed to retrieve categories' });
} else {
res.json(results);
}
});
});
// 新增分类接口
router.post('/', (req, res) => {
const { name } = req.body;
pool.query('INSERT INTO categories (name) VALUES (?)', [name], (error, results) => {
if (error) {
console.error('Error creating category: ', error);
res.status(500).json({ error: 'Failed to create category' });
} else {
res.json({ id: results.insertId, name });
}
});
});
// 修改分类接口
router.put('/:id', (req, res) => {
const { id } = req.params;
const { name } = req.body;
pool.query('UPDATE categories SET name = ? WHERE id = ?', [name, id], (error, results) => {
if (error) {
console.error('Error updating category: ', error);
res.status(500).json({ error: 'Failed to update category' });
} else if (results.affectedRows === 0) {
res.status(404).json({ error: 'Category not found' });
} else {
res.json({ id, name });
}
});
});
// 删除分类接口
router.delete('/:id', (req, res) => {
const { id } = req.params;
pool.query('DELETE FROM categories WHERE id = ?', [id], (error, results) => {
if (error) {
console.error('Error deleting category: ', error);
res.status(500).json({ error: 'Failed to delete category' });
} else if (results.affectedRows === 0) {
res.status(404).json({ error: 'Category not found' });
} else {
res.json({ message: 'Category deleted successfully' });
}
});
});
// 查看分类接口
router.get('/:id', (req, res) => {
const { id } = req.params;
pool.query('SELECT * FROM categories WHERE id = ?', [id], (error, results) => {
if (error) {
console.error('Error retrieving category: ', error);
res.status(500).json({ error: 'Failed to retrieve category' });
} else if (results.length === 0) {
res.status(404).json({ error: 'Category not found' });
} else {
res.json(results[0]);
}
});
});
module.exports = router;
```
然后,在主应用程序文件中引入分类模块,并将其挂载到相应的路由路径上:
```javascript
// app.js
const express = require('express');
const app = express();
const categoriesRouter = require('./categories');
app.use(express.json());
app.use('/api/categories', categoriesRouter);
// 其他中间件和路由...
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
```
以上示例代码基于 Express 框架和 MySQL 数据库,实现了分类管理后端接口的基本功能,包括获取分类列表、新增分类、修改分类、删除分类和查看分类。您可以根据实际需求进行调整和扩展,同时确保数据库连接等配置正确设置。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)