- 前端(HTML + JavaScript)0512px.cn
index.html
html
游戏商城
<script>
fetch('/games')
.then(response => response.json())
.then(data => {
const gamesList = document.getElementById('gamesList');
data.forEach(game => {
const listItem = document.createElement('li');
listItem.textContent = `${game.name} -
$$
{game.price}`;
gamesList.appendChild(listItem);
});
})
.catch(error => console.error(‘Error:’, error));
app.js
javascript
const express = require(‘express’);
const mongoose = require(‘mongoose’);
const bodyParser = require(‘body-parser’);
const app = express();
const PORT = 3000;
// 连接到MongoDB
mongoose.connect(‘mongodb://localhost:27017/gameStoreDB’, {
useNewUrlParser: true,
useUnifiedTopology: true
});
const Game = mongoose.model(‘Game’, new mongoose.Schema({
name: String,
price: Number
}));
// 中间件
app.use(bodyParser.json());
// 获取游戏列表
app.get(‘/games’, (req, res) => {
Game.find({}, (err, games) => {
if (err) return res.status(500).send(err);
res.json(games);
});
});
// 监听端口
app.listen(PORT, () => {
console.log(Server running on port ${PORT}
);
});
3. 数据库(MongoDB)
确保MongoDB已安装并运行在你的机器上。对于本示例,假设你已经有了一个名为gameStoreDB的数据库,并且其中有一个名为games的集合(collection),包含了一些游戏数据。
完整部署
安装依赖:在包含app.js的目录中运行npm init -y来创建package.json,然后运行npm install express mongoose body-parser来安装所需的npm包。
启动MongoDB:确保MongoDB服务正在运行。
运行后端:在命令行中,使用node app.js来启动你的Express应用。
访问前端:在浏览器中打开http://localhost:3000/index.html(确保你的index.html文件在正确的位置,或通过Express静态文件服务提供)。
这个示例提供了游戏商城的一个非常基础的实现。在实际应用中,你可能需要添加更多的功能,如用户认证、支付集成、更复杂的游戏数据管理等。由于创建一个完整的游戏商城系统涉及多个方面(如前端展示、后端逻辑、数据库管理等),并且每种语言都有其特定的用途和优势,我将提供一个简化的示例,涵盖前端(HTML + JavaScript)、后端(Node.js + Express)和数据库(MongoDB)的基本框架。
- 前端(HTML + JavaScript)
index.html
html
游戏商城
<script>
fetch('/games')
.then(response => response.json())
.then(data => {
const gamesList = document.getElementById('gamesList');
data.forEach(game => {
const listItem = document.createElement('li');
listItem.textContent = `${game.name} -
$$
{game.price}`;
gamesList.appendChild(listItem);
});
})
.catch(error => console.error(‘Error:’, error));
app.js
javascript
const express = require(‘express’);
const mongoose = require(‘mongoose’);
const bodyParser = require(‘body-parser’);
const app = express();
const PORT = 3000;
// 连接到MongoDB
mongoose.connect(‘mongodb://localhost:27017/gameStoreDB’, {
useNewUrlParser: true,
useUnifiedTopology: true
});
const Game = mongoose.model(‘Game’, new mongoose.Schema({
name: String,
price: Number
}));
// 中间件
app.use(bodyParser.json());
// 获取游戏列表
app.get(‘/games’, (req, res) => {
Game.find({}, (err, games) => {
if (err) return res.status(500).send(err);
res.json(games);
});
});
// 监听端口
app.listen(PORT, () => {
console.log(Server running on port ${PORT}
);
});
3. 数据库(MongoDB)
确保MongoDB已安装并运行在你的机器上。对于本示例,假设你已经有了一个名为gameStoreDB的数据库,并且其中有一个名为games的集合(collection),包含了一些游戏数据。
完整部署
安装依赖:在包含app.js的目录中运行npm init -y来创建package.json,然后运行npm install express mongoose body-parser来安装所需的npm包。
启动MongoDB:确保MongoDB服务正在运行。
运行后端:在命令行中,使用node app.js来启动你的Express应用。
访问前端:在浏览器中打开http://localhost:3000/index.html(确保你的index.html文件在正确的位置,或通过Express静态文件服务提供)。
这个示例提供了游戏商城的一个非常基础的实现。在实际应用中,你可能需要添加更多的功能,如用户认证、支付集成、更复杂的游戏数据管理等。