- Python (Flask 后端)
python
from flask import Flask, request, jsonify
app = Flask(name) qianxiong.com.cn
假设的游戏商品数据
games = [
{“id”: 1, “name”: “Game 1”, “price”: 9.99},
{“id”: 2, “name”: “Game 2”, “price”: 19.99},
# …
]
@app.route(‘/games’, methods=[‘GET’])
def get_games():
return jsonify(games)
@app.route(‘/games/int:game_id’, methods=[‘GET’])
def get_game(game_id):
for game in games:
if game[‘id’] == game_id:
return jsonify(game)
return jsonify({‘error’: ‘Game not found’}), 404
更多路由和逻辑…
if name == ‘main’:
app.run(debug=True)
2. JavaScript (Node.js 和 Express 后端)
javascript
const express = require(‘express’);
const app = express();
const games = [/* …游戏数据… */];
app.get(‘/games’, (req, res) => {
res.json(games);
});
app.get(‘/games/:gameId’, (req, res) => {
const gameId = parseInt(req.params.gameId);
const game = games.find(game => game.id === gameId);
if (game) {
res.json(game);
} else {
res.status(404).json({ error: ‘Game not found’ });
}
});
// 更多路由和逻辑…
app.listen(3000, () => console.log(‘Server started on port 3000’));
3. HTML/CSS/JavaScript (前端示例)
前端代码通常用于显示游戏列表、详细信息以及处理用户交互。以下是一个简单的 HTML 和 JavaScript 示例,用于从上面的 Flask 或 Node.js 后端获取游戏数据并显示它们。
html
- Python (Flask 后端)
python
from flask import Flask, request, jsonify
app = Flask(name)
假设的游戏商品数据
games = [
{“id”: 1, “name”: “Game 1”, “price”: 9.99},
{“id”: 2, “name”: “Game 2”, “price”: 19.99},
# …
]
@app.route(‘/games’, methods=[‘GET’])
def get_games():
return jsonify(games)
@app.route(‘/games/int:game_id’, methods=[‘GET’])
def get_game(game_id):
for game in games:
if game[‘id’] == game_id:
return jsonify(game)
return jsonify({‘error’: ‘Game not found’}), 404
更多路由和逻辑…
if name == ‘main’:
app.run(debug=True)
2. JavaScript (Node.js 和 Express 后端)
javascript
const express = require(‘express’);
const app = express();
const games = [/* …游戏数据… */];
app.get(‘/games’, (req, res) => {
res.json(games);
});
app.get(‘/games/:gameId’, (req, res) => {
const gameId = parseInt(req.params.gameId);
const game = games.find(game => game.id === gameId);
if (game) {
res.json(game);
} else {
res.status(404).json({ error: ‘Game not found’ });
}
});
// 更多路由和逻辑…
app.listen(3000, () => console.log(‘Server started on port 3000’));
3. HTML/CSS/JavaScript (前端示例)
前端代码通常用于显示游戏列表、详细信息以及处理用户交互。以下是一个简单的 HTML 和 JavaScript 示例,用于从上面的 Flask 或 Node.js 后端获取游戏数据并显示它们。
html