node.js商品管理系统简单的增删改查
时间: 2023-10-09 10:17:36 浏览: 44
好的,下面是一个简单的 node.js 商品管理系统的增删改查的代码示例:
```javascript
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
let products = [
{ id: 1, name: 'Product 1', price: 10 },
{ id: 2, name: 'Product 2', price: 20 },
{ id: 3, name: 'Product 3', price: 30 }
];
// 获取所有商品
app.get('/api/products', (req, res) => {
res.send(products);
});
// 获取单个商品
app.get('/api/products/:id', (req, res) => {
const id = parseInt(req.params.id);
const product = products.find(p => p.id === id);
if (!product) {
res.status(404).send('Product not found');
} else {
res.send(product);
}
});
// 添加商品
app.post('/api/products', (req, res) => {
const product = req.body;
product.id = products.length + 1;
products.push(product);
res.send(product);
});
// 修改商品
app.put('/api/products/:id', (req, res) => {
const id = parseInt(req.params.id);
const product = products.find(p => p.id === id);
if (!product) {
res.status(404).send('Product not found');
} else {
const newProduct = req.body;
newProduct.id = id;
const index = products.indexOf(product);
products[index] = newProduct;
res.send(newProduct);
}
});
// 删除商品
app.delete('/api/products/:id', (req, res) => {
const id = parseInt(req.params.id);
const product = products.find(p => p.id === id);
if (!product) {
res.status(404).send('Product not found');
} else {
const index = products.indexOf(product);
products.splice(index, 1);
res.send(product);
}
});
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Listening on port ${port}...`));
```
使用上述代码,可以通过以下方式进行增删改查等操作:
- 获取所有商品:发送 GET 请求到 `/api/products`。
- 获取单个商品:发送 GET 请求到 `/api/products/:id`,其中 `:id` 为商品的 ID。
- 添加商品:发送 POST 请求到 `/api/products`,请求体包含新商品的信息。
- 修改商品:发送 PUT 请求到 `/api/products/:id`,其中 `:id` 为商品的 ID,请求体包含修改后的商品信息。
- 删除商品:发送 DELETE 请求到 `/api/products/:id`,其中 `:id` 为商品的 ID。
以上是一个简单的 node.js 商品管理系统的增删改查的代码示例,你可以根据自己的需求进行修改和扩展。
相关推荐
![.zip](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)