- Python (后端示例)gdtouhaozhoupu.cn
使用Flask框架:
python
from flask import Flask, request, jsonify
app = Flask(name)
假设有一个游戏列表的字典
games = {
1: {‘name’: ‘Game 1’, ‘price’: 10.99},
2: {‘name’: ‘Game 2’, ‘price’: 20.99},
# …更多游戏
}
@app.route(‘/games’, methods=[‘GET’])
def get_games():
return jsonify(list(games.values()))
@app.route(‘/games/int:game_id’, methods=[‘GET’])
def get_game(game_id):
if game_id in games:
return jsonify(games[game_id])
else:
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 = { /* …同Python示例中的games字典… */ };
app.get(‘/games’, (req, res) => {
res.json(Object.values(games));
});
app.get(‘/games/:gameId’, (req, res) => {
const gameId = parseInt(req.params.gameId);
if (gameId in games) {
res.json(games[gameId]);
} else {
res.status(404).json({ error: ‘Game not found’ });
}
});
// 启动应用…
app.listen(3000, () => console.log(‘Server running on port 3000’));
3. HTML/CSS/JavaScript (前端示例)
这里只展示一个简单的HTML页面结构和一个JavaScript函数来从后端获取游戏列表(使用Fetch API):
html
<script>
fetch('/games')
.then(response => response.json())
.then(games => {
const gameList = document.getElementById('gameList');
games.forEach(game => {
// 假设我们有一个简单的列表项模板
const listItem = document.createElement('li');
listItem.textContent = `${game.name} -
$$
{game.price}`;
gameList.appendChild(listItem);
});
})
.catch(error => console.error(‘Error fetching games:’, error));
- Python (后端示例)
使用Flask框架:
python
from flask import Flask, request, jsonify
app = Flask(name)
假设有一个游戏列表的字典
games = {
1: {‘name’: ‘Game 1’, ‘price’: 10.99},
2: {‘name’: ‘Game 2’, ‘price’: 20.99},
# …更多游戏
}
@app.route(‘/games’, methods=[‘GET’])
def get_games():
return jsonify(list(games.values()))
@app.route(‘/games/int:game_id’, methods=[‘GET’])
def get_game(game_id):
if game_id in games:
return jsonify(games[game_id])
else:
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 = { /* …同Python示例中的games字典… */ };
app.get(‘/games’, (req, res) => {
res.json(Object.values(games));
});
app.get(‘/games/:gameId’, (req, res) => {
const gameId = parseInt(req.params.gameId);
if (gameId in games) {
res.json(games[gameId]);
} else {
res.status(404).json({ error: ‘Game not found’ });
}
});
// 启动应用…
app.listen(3000, () => console.log(‘Server running on port 3000’));
3. HTML/CSS/JavaScript (前端示例)
这里只展示一个简单的HTML页面结构和一个JavaScript函数来从后端获取游戏列表(使用Fetch API):
html
<script>
fetch('/games')
.then(response => response.json())
.then(games => {
const gameList = document.getElementById('gameList');
games.forEach(game => {
// 假设我们有一个简单的列表项模板
const listItem = document.createElement('li');
listItem.textContent = `${game.name} -
$$
{game.price}`;
gameList.appendChild(listItem);
});
})
.catch(error => console.error(‘Error fetching games:’, error));