首页 > 大数据 > 数据挖掘 > 电商平台商品API:商品信息同步与上架管
在电商平台开发中,商品信息的同步与上架管理是非常关键的功能。通过API接口,可以方便地实现商品信息的同步和上架操作。以下是一个简单的示例,展示了如何使用API来实现这些功能。假设我们有一个电商平台,其中有一个商品管理系统,我们需要编写一个API来同步和上架商品信息。
首先,确保你已经安装了Node.js和MongoDB。然后,初始化一个新的Node.js项目并安装所需的依赖:
bash复制代码
|
|
mkdir ecommerce-api |
|
|
cd ecommerce-api |
|
|
npm init -y |
|
|
npm install express mongoose axios body-parser |
我们定义一个简单的商品模型。创建一个名为
models/Product.js的文件:
javascript复制代码
|
|
const mongoose = require('mongoose'); |
|
|
|
|
|
const productSchema = new mongoose.Schema({ |
|
|
name: String, |
|
|
description: String, |
|
|
price: Number, |
|
|
stock: Number, |
|
|
isActive: { type: Boolean, default: false }, // 是否上架 |
|
|
createdAt: { type: Date, default: Date.now } |
|
|
}); |
|
|
|
|
|
module.exports = mongoose.model('Product', productSchema); |
创建一个名为
server.js的文件,并设置Express服务器:
javascript复制代码
|
|
const express = require('express'); |
|
|
const mongoose = require('mongoose'); |
|
|
const bodyParser = require('body-parser'); |
|
|
const Product = require('./models/Product'); |
|
|
const axios = require('axios'); |
|
|
|
|
|
const app = express(); |
|
|
const port = 3000; |
|
|
|
|
|
// 连接MongoDB |
|
|
mongoose.connect('mongodb://localhost:27017/ecommerce', { |
|
|
useNewUrlParser: true, |
|
|
useUnifiedTopology: true |
|
|
}); |
|
|
|
|
|
// 中间件 |
|
|
app.use(bodyParser.json()); |
|
|
|
|
|
// 商品同步API |
|
|
app.post('/sync-products', async (req, res) => { |
|
|
const { externalProductId, externalUrl } = req.body; |
|
|
|
|
|
try { |
|
|
// 从外部API获取商品信息 |
|
|
const response = await axios.get(externalUrl); |
|
|
const externalProduct = response.data; |
|
|
|
|
|
// 保存到数据库 |
|
|
const product = new Product({ |
|
|
name: externalProduct.name, |
|
|
description: externalProduct.description, |
|
|
price: externalProduct.price, |
|
|
stock: externalProduct.stock, |
|
|
isActive: false // 初始状态为未上架 |
|
|
}); |
|
|
|
|
|
await product.save(); |
|
|
|
|
|
// 更新外部商品ID |
|
|
await Product.findByIdAndUpdate(product._id, { externalProductId }, { new: true }); |
|
|
|
|
|
res.status(201).send({ message: 'Product synced successfully', product }); |
|
|
} catch (error) { |
|
|
res.status(500).send({ error: error.message }); |
|
|
} |
|
|
}); |
|
|
|
|
|
// 商品上架API |
|
|
app.put('/上架/:productId', async (req, res) => { |
|
|
const { productId } = req.params; |
|
|
|
|
|
try { |
|
|
const product = await Product.findByIdAndUpdate(productId, { isActive: true }, { new: true }); |
|
|
|
|
|
if (!product) { |
|
|
return res.status(404).send({ message: 'Product not found' }); |
|
|
} |
|
|
|
|
|
res.send({ message: 'Product listed successfully', product }); |
|
|
} catch (error) { |
|
|
res.status(500).send({ error: error.message }); |
|
|
} |
|
|
}); |
|
|
|
|
|
// 启动服务器 |
|
|
app.listen(port, () => { |
|
|
console.log(`Server running at http://localhost:${port}`); |
|
|
}); |
使用curl或Postman发送一个POST请求到
/sync-products,带上外部商品ID和URL:
bash复制代码
|
|
curl -X POST http://localhost:3000/sync-products \ |
|
|
-H "Content-Type: application/json" \ |
|
|
-d '{ |
|
|
"externalProductId": "12345", |
|
|
"externalUrl": "https://api.example.com/products/12345" |
|
|
}' |
使用curl或Postman发送一个PUT请求到
/上架/:productId,将商品上架:
bash复制代码
|
|
curl -X PUT http://localhost:3000/上架/60f0b8e0c50b4a2e74f0f63a \ |
|
|
-H "Content-Type: application/json" |
这个示例展示了如何使用Node.js和Express搭建一个简单的电商平台商品API,实现商品信息的同步与上架管理。你可以根据实际需求进行扩展和优化。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70042344/viewspace-3065606/ ,如需转载,请注明出处,否则将追究法律责任。