- Python (Flask 框架, 示例后端 API)gzrrgx.com
python
from flask import Flask, jsonify, request
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):
game = next((item for item in games if item[“id”] == game_id), None)
if game:
return jsonify(game)
else:
return jsonify({“error”: “Game not found”}), 404
启动 Flask 应用(需要额外配置和运行)
if name == ‘main’:
app.run(debug=True)
2. JavaScript (Node.js + Express, 示例后端 API)
javascript
const express = require(‘express’);
const app = express();
const port = 3000;
// 假设的游戏商品数据
const 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 game_id = parseInt(req.params.game_id);
const game = games.find(game => game.id === game_id);
if (game) {
res.json(game);
} else {
res.status(404).json({error: ‘Game not found’});
}
});
app.listen(port, () => {
console.log(App listening at http://localhost:${port}
);
});
3. HTML/CSS/JavaScript (前端示例)
这里仅展示 HTML 和 JavaScript 的基本结构,用于从后端 API 获取数据并展示。
html
<script>
fetch('/games')
.then(response => response.json())
.then(games => {
const container = document.getElementById('games-container');
games.forEach(game => {
const gameElement = document.createElement('div');
gameElement.textContent = `Name: ${game.name}, Price:
$$
{game.price}`;
container.appendChild(gameElement);
});
})
.catch(error => console.error(‘Error:’, error));
java
import org.springframework.web.bind.annotation.*;
import java.util.List;
// …(省略其他必要的 Spring Boot 配置)
@RestController
@RequestMapping(“/games”)
public class GameController {
// 假设的游戏商品数据(实际中应使用数据库)
private List<Game> games = // ... 初始化游戏列表
@GetMapping
public List<Game> getAllGames() {
return games;
}
@GetMapping("/{gameId}")
public Game getGameById(@PathVariable Long gameId) {
// ...
由于编写一个完整的游戏商城代码会涉及到多个部分(如前端、后端、数据库等),并且篇幅有限,我将为你提供简化版的伪代码或代码片段,分别用几种流行的编程语言来展示。
- Python (Flask 框架, 示例后端 API)
python
from flask import Flask, jsonify, request
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):
game = next((item for item in games if item[“id”] == game_id), None)
if game:
return jsonify(game)
else:
return jsonify({“error”: “Game not found”}), 404
启动 Flask 应用(需要额外配置和运行)
if name == ‘main’:
app.run(debug=True)
2. JavaScript (Node.js + Express, 示例后端 API)
javascript
const express = require(‘express’);
const app = express();
const port = 3000;
// 假设的游戏商品数据
const 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 game_id = parseInt(req.params.game_id);
const game = games.find(game => game.id === game_id);
if (game) {
res.json(game);
} else {
res.status(404).json({error: ‘Game not found’});
}
});
app.listen(port, () => {
console.log(App listening at http://localhost:${port}
);
});
3. HTML/CSS/JavaScript (前端示例)
这里仅展示 HTML 和 JavaScript 的基本结构,用于从后端 API 获取数据并展示。
html
<script>
fetch('/games')
.then(response => response.json())
.then(games => {
const container = document.getElementById('games-container');
games.forEach(game => {
const gameElement = document.createElement('div');
gameElement.textContent = `Name: ${game.name}, Price:
$$
{game.price}`;
container.appendChild(gameElement);
});
})
.catch(error => console.error(‘Error:’, error));
java
import org.springframework.web.bind.annotation.*;
import java.util.List;
// …(省略其他必要的 Spring Boot 配置)
@RestController
@RequestMapping(“/games”)
public class GameController {
// 假设的游戏商品数据(实际中应使用数据库)
private List<Game> games = // ... 初始化游戏列表
@GetMapping
public List<Game> getAllGames() {
return games;
}
@GetMapping("/{gameId}")
public Game getGameById(@PathVariable Long gameId) {
// ...