- Python (Flask 后端框架)gwygov.cn
python
from flask import Flask, render_template, jsonify
app = Flask(name)
假设的商品数据
games = [
{“id”: 1, “name”: “Game 1”, “price”: 9.99},
{“id”: 2, “name”: “Game 2”, “price”: 19.99},
# …
]
@app.route(‘/’)
def index():
return render_template(‘index.html’, games=games)
if name == ‘main’:
app.run(debug=True)
2. JavaScript (Node.js + Express 后端框架)
javascript
const express = require(‘express’);
const app = express();
// 假设的商品数据
const games = [
{id: 1, name: ‘Game 1’, price: 9.99},
{id: 2, name: ‘Game 2’, price: 19.99},
// …
];
app.get(‘/’, (req, res) => {
res.send(games); // 通常这里会发送HTML模板或JSON数据
});
app.listen(3000, () => console.log(‘Server started on port 3000’));
3. Java (Spring Boot 后端框架)
这里只展示一个简化的Controller示例:
java
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.List;
@RestController
public class GameController {
// 假设的商品数据
private List<Game> games = Arrays.asList(
new Game(1, "Game 1", 9.99),
new Game(2, "Game 2", 19.99)
// ...
);
@GetMapping("/")
public List<Game> getGames() {
return games;
}
// 简单的Game类
static class Game {
private int id;
private String name;
private double price;
// 构造器、getter和setter...
}
}
前端 (HTML + JavaScript)
这里是一个简单的HTML页面,使用JavaScript从后端获取商品数据并显示:
html
Game Store
<script>
fetch('/') // 假设后端运行在本地,并返回JSON数据
.then(response => response.json())
.then(games => {
const gameList = document.getElementById('gameList');
games.forEach(game => {
const li = document.createElement('li');
li.textContent = `${game.name} -
$$
{game.price.toFixed(2)}`;
gameList.appendChild(li);
});
});
- Python (Flask 后端框架)
python
from flask import Flask, render_template, jsonify
app = Flask(name)
假设的商品数据
games = [
{“id”: 1, “name”: “Game 1”, “price”: 9.99},
{“id”: 2, “name”: “Game 2”, “price”: 19.99},
# …
]
@app.route(‘/’)
def index():
return render_template(‘index.html’, games=games)
if name == ‘main’:
app.run(debug=True)
2. JavaScript (Node.js + Express 后端框架)
javascript
const express = require(‘express’);
const app = express();
// 假设的商品数据
const games = [
{id: 1, name: ‘Game 1’, price: 9.99},
{id: 2, name: ‘Game 2’, price: 19.99},
// …
];
app.get(‘/’, (req, res) => {
res.send(games); // 通常这里会发送HTML模板或JSON数据
});
app.listen(3000, () => console.log(‘Server started on port 3000’));
3. Java (Spring Boot 后端框架)
这里只展示一个简化的Controller示例:
java
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.List;
@RestController
public class GameController {
// 假设的商品数据
private List<Game> games = Arrays.asList(
new Game(1, "Game 1", 9.99),
new Game(2, "Game 2", 19.99)
// ...
);
@GetMapping("/")
public List<Game> getGames() {
return games;
}
// 简单的Game类
static class Game {
private int id;
private String name;
private double price;
// 构造器、getter和setter...
}
}
前端 (HTML + JavaScript)
这里是一个简单的HTML页面,使用JavaScript从后端获取商品数据并显示:
html
Game Store
<script>
fetch('/') // 假设后端运行在本地,并返回JSON数据
.then(response => response.json())
.then(games => {
const gameList = document.getElementById('gameList');
games.forEach(game => {
const li = document.createElement('li');
li.textContent = `${game.name} -
$$
{game.price.toFixed(2)}`;
gameList.appendChild(li);
});
});