- Python (Flask) 后端示例etym.cn
python
from flask import Flask, request, jsonify
app = Flask(name)
模拟数据库商品数据
products = [
{“id”: 1, “name”: “Apple”, “price”: 100},
{“id”: 2, “name”: “Banana”, “price”: 50},
]
@app.route(‘/products’, methods=[‘GET’])
def get_products():
return jsonify(products)
@app.route(‘/products/int:product_id’, methods=[‘GET’])
def get_product(product_id):
for product in products:
if product[‘id’] == product_id:
return jsonify(product)
return jsonify({“error”: “Product not found”}), 404
… 其他路由和逻辑
if name == ‘main’:
app.run(debug=True)
2. JavaScript (Node.js + Express) 后端示例
javascript
const express = require(‘express’);
const app = express();
const port = 3000;
// 模拟数据库商品数据
let products = [
{id: 1, name: ‘Apple’, price: 100},
{id: 2, name: ‘Banana’, price: 50},
];
app.get(‘/products’, (req, res) => {
res.json(products);
});
app.get(‘/products/:product_id’, (req, res) => {
const productId = parseInt(req.params.product_id);
const product = products.find(p => p.id === productId);
if (product) {
res.json(product);
} else {
res.status(404).json({error: ‘Product not found’});
}
});
// … 其他路由和逻辑
app.listen(port, () => {
console.log(App listening at http://localhost:${port}
);
});
3. HTML 前端示例(仅展示一个商品列表)
html
Products
- Apple - $100
- Banana - $50
<!-- 引入JavaScript文件来添加动态功能(如Ajax请求) -->
<script src="your-script.js"></script>
4. SQL 数据库示例(仅展示表结构) sql CREATE TABLE products ( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255) NOT NULL, price DECIMAL(10, 2) NOT NULL );
– 插入示例数据
INSERT INTO products (name, price) VALUES (‘Apple’, 100.00);
INSERT INTO products (name, price) VALUES (‘Banana’, 50.00);
请注意,这些只是非常基础的示例,一个真实的购物商城会涉及更多的功能和复杂性,如用户认证、购物车管理、订单处理、支付集成、库存管理等。由于一个完整的购物商城代码涉及多个部分(前端、后端、数据库等),并且会相当复杂,因此在这里我将为你提供几个简单示例,分别用Python(Flask框架,后端)、JavaScript(Node.js + Express框架,后端)、HTML(前端)和SQL(数据库)来展示基本概念。
- Python (Flask) 后端示例
python
from flask import Flask, request, jsonify
app = Flask(name)
模拟数据库商品数据
products = [
{“id”: 1, “name”: “Apple”, “price”: 100},
{“id”: 2, “name”: “Banana”, “price”: 50},
]
@app.route(‘/products’, methods=[‘GET’])
def get_products():
return jsonify(products)
@app.route(‘/products/int:product_id’, methods=[‘GET’])
def get_product(product_id):
for product in products:
if product[‘id’] == product_id:
return jsonify(product)
return jsonify({“error”: “Product not found”}), 404
… 其他路由和逻辑
if name == ‘main’:
app.run(debug=True)
2. JavaScript (Node.js + Express) 后端示例
javascript
const express = require(‘express’);
const app = express();
const port = 3000;
// 模拟数据库商品数据
let products = [
{id: 1, name: ‘Apple’, price: 100},
{id: 2, name: ‘Banana’, price: 50},
];
app.get(‘/products’, (req, res) => {
res.json(products);
});
app.get(‘/products/:product_id’, (req, res) => {
const productId = parseInt(req.params.product_id);
const product = products.find(p => p.id === productId);
if (product) {
res.json(product);
} else {
res.status(404).json({error: ‘Product not found’});
}
});
// … 其他路由和逻辑
app.listen(port, () => {
console.log(App listening at http://localhost:${port}
);
});
3. HTML 前端示例(仅展示一个商品列表)
html
Products
- Apple - $100
- Banana - $50
<!-- 引入JavaScript文件来添加动态功能(如Ajax请求) -->
<script src="your-script.js"></script>
4. SQL 数据库示例(仅展示表结构) sql CREATE TABLE products ( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255) NOT NULL, price DECIMAL(10, 2) NOT NULL );
– 插入示例数据
INSERT INTO products (name, price) VALUES (‘Apple’, 100.00);
INSERT INTO products (name, price) VALUES (‘Banana’, 50.00);
请注意,这些只是非常基础的示例,一个真实的购物商城会涉及更多的功能和复杂性,如用户认证、购物车管理、订单处理、支付集成、库存管理等。