- Python (Flask) 后端示例
python
from flask import Flask, request, jsonify
app = Flask(name) dgynx.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();
app.use(express.json());
// 假设的游戏商品列表
let games = [
{id: 1, name: ‘Game 1’, price: 9.99},
{id: 2, name: ‘Game 2’, price: 19.99},
// …
];
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 is running on port 3000’));
3. HTML/CSS/JavaScript 前端示例(仅展示获取游戏列表的简化版)
html
Game Store
<script>
fetch('/games')
.then(response => response.json())
.then(games => {
const gameList = document.getElementById('game-list');
games.forEach(game => {
const li = document.createElement('li');
li.textContent = `${game.name} -
$$
{game.price}`;
gameList.appendChild(li);
});
})
.catch(error => console.error(‘Error:’, error));
- 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();
app.use(express.json());
// 假设的游戏商品列表
let games = [
{id: 1, name: ‘Game 1’, price: 9.99},
{id: 2, name: ‘Game 2’, price: 19.99},
// …
];
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 is running on port 3000’));
3. HTML/CSS/JavaScript 前端示例(仅展示获取游戏列表的简化版)
html
Game Store
<script>
fetch('/games')
.then(response => response.json())
.then(games => {
const gameList = document.getElementById('game-list');
games.forEach(game => {
const li = document.createElement('li');
li.textContent = `${game.name} -
$$
{game.price}`;
gameList.appendChild(li);
});
})
.catch(error => console.error(‘Error:’, error));