- Python (使用Flask框架)
安装Flask:
bash
pip install Flask
示例代码:
python
from flask import Flask, jsonify, request
app = Flask(name)
假设的商品列表
products = [
{“id”: 1, “name”: “苹果”, “price”: 10},
{“id”: 2, “name”: “香蕉”, “price”: 5},
]
@app.route(‘/products’, methods=[‘GET’])
def get_products():
return jsonify(products)
@app.route(‘/products’, methods=[‘POST’])
def add_product():
data = request.json
new_product = {“id”: len(products) + 1, “name”: data[‘name’], “price”: data[‘price’]}
products.append(new_product)
return jsonify(new_product), 201
if name == ‘main’:
app.run(debug=True)
2. JavaScript (使用Node.js和Express框架)
安装Node.js和Express:
bash
npm init -y
npm install express body-parser
示例代码:
javascript
const express = require(‘express’);
const bodyParser = require(‘body-parser’);
const app = express();
const port = 3000;
app.use(bodyParser.json());
let products = [
{id: 1, name: ‘苹果’, price: 10},
{id: 2, name: ‘香蕉’, price: 5},
];
app.get(‘/products’, (req, res) => {
res.json(products);
});
app.post(‘/products’, (req, res) => {
const newProduct = {
id: products.length + 1,
name: req.body.name,
price: req.body.price
};
products.push(newProduct);
res.status(201).json(newProduct);
});
app.listen(port, () => {
console.log(Server running on port ${port}
);
});
3. Java (使用Spring Boot)
创建一个Spring Boot项目(通常使用Spring Initializr)
ProductController.java:
java
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping(“/products”)
public class ProductController {
private List<Product> products = new ArrayList<>();
static {
products.add(new Product(1, "苹果", 10));
products.add(new Product(2, "香蕉", 5));
}
@GetMapping
public List<Product> getAllProducts() {
return products;
}
@PostMapping
public Product addProduct(@RequestBody Product product) {
product.setId(products.size() + 1);
products.add(product);
return product;
}
static class Product {
private int id;
private String name;
private double price;
// 构造函数、getter和setter省略
}
}
这些示例提供了一个非常基础的购物商城后端框架,你可以在此基础上添加更多功能,如用户认证、支付处理、数据库集成等。每种语言都有其丰富的库和框架来支持复杂的Web应用开发。由于直接在一个回答中完整地展示一个购物商城的代码(尤其是在多种编程语言中)是不切实际的,我将为你概述如何用几种流行的编程语言(如Python、JavaScript (Node.js)、和Java)来构建购物商城的基本框架。每个示例将仅覆盖后端逻辑的一部分,因为前端通常使用HTML/CSS/JavaScript构建,并且可以通过任何后端服务提供数据。
- Python (使用Flask框架)
安装Flask:
bash
pip install Flask
示例代码:
python
from flask import Flask, jsonify, request
app = Flask(name)
假设的商品列表
products = [
{“id”: 1, “name”: “苹果”, “price”: 10},
{“id”: 2, “name”: “香蕉”, “price”: 5},
]
@app.route(‘/products’, methods=[‘GET’])
def get_products():
return jsonify(products)
@app.route(‘/products’, methods=[‘POST’])
def add_product():
data = request.json
new_product = {“id”: len(products) + 1, “name”: data[‘name’], “price”: data[‘price’]}
products.append(new_product)
return jsonify(new_product), 201
if name == ‘main’:
app.run(debug=True)
2. JavaScript (使用Node.js和Express框架)
安装Node.js和Express:
bash
npm init -y
npm install express body-parser
示例代码:
javascript
const express = require(‘express’);
const bodyParser = require(‘body-parser’);
const app = express();
const port = 3000;
app.use(bodyParser.json());
let products = [
{id: 1, name: ‘苹果’, price: 10},
{id: 2, name: ‘香蕉’, price: 5},
];
app.get(‘/products’, (req, res) => {
res.json(products);
});
app.post(‘/products’, (req, res) => {
const newProduct = {
id: products.length + 1,
name: req.body.name,
price: req.body.price
};
products.push(newProduct);
res.status(201).json(newProduct);
});
app.listen(port, () => {
console.log(Server running on port ${port}
);
});
3. Java (使用Spring Boot)
创建一个Spring Boot项目(通常使用Spring Initializr)
ProductController.java:
java
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping(“/products”)
public class ProductController {
private List<Product> products = new ArrayList<>();
static {
products.add(new Product(1, "苹果", 10));
products.add(new Product(2, "香蕉", 5));
}
@GetMapping
public List<Product> getAllProducts() {
return products;
}
@PostMapping
public Product addProduct(@RequestBody Product product) {
product.setId(products.size() + 1);
products.add(product);
return product;
}
static class Product {
private int id;
private String name;
private double price;
// 构造函数、getter和setter省略
}
}
这些示例提供了一个非常基础的购物商城后端框架,你可以在此基础上添加更多功能,如用户认证、支付处理、数据库集成等。每种语言都有其丰富的库和框架来支持复杂的Web应用开发。