Python (Flask) 后端示例
python
from flask import Flask, request, jsonify
app = Flask(name) wxyrsy.com
假设的游戏商品列表
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)
JavaScript (Node.js + Express) 后端示例
javascript
const express = require(‘express’);
const app = express();
const bodyParser = require(‘body-parser’);
app.use(bodyParser.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/:game_id’, (req, res) => {
const gameId = parseInt(req.params.game_id);
const game = games.find(game => game.id === gameId);
if (game) {
res.json(game);
} else {
res.status(404).json({error: ‘Game not found’});
}
});
// … 其他路由
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(Server is running on port ${PORT}.
);
});
HTML/CSS/JavaScript 前端示例
这只是一个非常基础的HTML页面,用于显示游戏列表。实际的商城页面会更复杂,包含购物车、结账等功能。
html
Game Store
<script> // 假设从后端API获取游戏列表的伪代码 fetch('/games') .then(response => response.json()) .then(games => { const gameList = document.getElementById('game-list'); games.forEach(game => { const listItem = document.createElement('li'); listItem.textContent = `${game.name} -
$$
{game.price}`;
gameList.appendChild(listItem);
});
})
.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)
JavaScript (Node.js + Express) 后端示例
javascript
const express = require(‘express’);
const app = express();
const bodyParser = require(‘body-parser’);
app.use(bodyParser.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/:game_id’, (req, res) => {
const gameId = parseInt(req.params.game_id);
const game = games.find(game => game.id === gameId);
if (game) {
res.json(game);
} else {
res.status(404).json({error: ‘Game not found’});
}
});
// … 其他路由
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(Server is running on port ${PORT}.
);
});
HTML/CSS/JavaScript 前端示例
这只是一个非常基础的HTML页面,用于显示游戏列表。实际的商城页面会更复杂,包含购物车、结账等功能。
html
Game Store
<script> // 假设从后端API获取游戏列表的伪代码 fetch('/games') .then(response => response.json()) .then(games => { const gameList = document.getElementById('game-list'); games.forEach(game => { const listItem = document.createElement('li'); listItem.textContent = `${game.name} -
$$
{game.price}`;
gameList.appendChild(listItem);
});
})
.catch(error => console.error(‘Error:’, error));