From 9bf6a1c508f34d7be435f9fae2660a793bae6ea7 Mon Sep 17 00:00:00 2001 From: KUN1007 Date: Fri, 2 Jun 2023 22:24:57 +0800 Subject: [PATCH] rebuild server --- server/index.js | 362 ++------------------------------------ server/routes/login.js | 21 +++ server/routes/register.js | 23 +++ server/routes/topic.js | 267 ++++++++++++++++++++++++++++ server/routes/user.js | 68 +++++++ 5 files changed, 391 insertions(+), 350 deletions(-) create mode 100644 server/routes/login.js create mode 100644 server/routes/register.js create mode 100644 server/routes/topic.js create mode 100644 server/routes/user.js diff --git a/server/index.js b/server/index.js index f3d97dfe..04555f50 100644 --- a/server/index.js +++ b/server/index.js @@ -9,360 +9,22 @@ app.use(bodyParser.json()) const port = 10007 // 端口号 -// 模拟一个 topics 数组 -const topics = [ - // 帖子的数组 - { - topicId: 1, - topicTitle: '啊这可海星啊这可海星啊这可海星啊这可海星啊这可海星', - topicFloorCount: 1, - topicContent: '

啊这可海星

', - topicPublishTime: Date.now(), - topicPopularity: 100, - topicTags: ['啊这可海星', '啊这可海星'], - topicUpvotes: 1007, - topicViews: 10007, - topicLikes: 1007, - topicDislikes: 17, - topicPartition: ['galgame'], - topicReplies: { - reply: [ - // 存放了所有帖子的回复 - { - replyId: 1, - replier: { - avatar: './assets/images/topic.jpg', - name: '鲲鲲鲲', - cutenessPoints: 107, - }, - repliedTo: 'KUN', - isEdited: true, - editedTimestamp: Date.now(), - replyTime: Date.now(), - /* 此处过滤掉文本样式,评论只保留纯文本 */ - replyContent: '鲲鲲鲲', - replyUpvotes: 1, - replyLikes: 100, - replyDislikes: 10, - replyComments: [ - { - commentId: 1, - commenterAvatar: './assets/images/favicon.png', - commenterName: '啊这可海星', - commentedTo: 'azkhx', - commentUpvotes: 5, - commentDislikes: 1, - commentContent: '鲲鲲鲲鲲鲲,啊这可海星', - }, - { - commentId: 2, - commenterAvatar: './assets/images/favicon.png', - commenterName: '啊这可海星', - commentedTo: 'azkhx', - commentUpvotes: 5, - commentDislikes: 1, - commentContent: '鲲鲲鲲鲲鲲,啊这可海星', - }, - { - commentId: 3, - commenterAvatar: './assets/images/favicon.png', - commenterName: '啊这可海星', - commentedTo: 'azkhx', - commentUpvotes: 5, - commentDislikes: 1, - commentContent: '鲲鲲鲲鲲鲲,啊这可海星', - }, - ], - replyCount: 1, - }, - { - replyId: 2, - replier: { - avatar: './assets/images/topic.jpg', - name: 'KUN', - cutenessPoints: 107, - }, - repliedTo: 'KUN', - isEdited: true, - editedTimestamp: Date.now(), - replyTime: Date.now(), - replyContent: '鲲鲲鲲', - replyUpvotes: 1, - replyLikes: 100, - replyDislikes: 10, - replyComments: [ - { - commentId: 1, - commenterAvatar: './assets/images/favicon.png', - commenterName: '啊这可海星', - commentedTo: '鲲鲲鲲', - commentUpvotes: 5, - commentDislikes: 1, - /* 此处过滤掉文本样式,评论只保留纯文本 */ - commentContent: '鲲鲲鲲鲲鲲,啊这可海星', - }, - ], - replyCount: 1, - }, - ], - replyCount: 1, - }, - topicStatus: '正常', - topicIsEdited: false, - topicAuthor: { - avatar: './assets/images/KUN.jpg', - name: 'KUN', - cutenessPoints: 1007, - }, - }, - { - topicId: 2, - topicTitle: '啊这可海星啊这可海星啊这可海星啊这可海星啊这可海星', - topicFloorCount: 1, - topicContent: '

啊这可海星

', - topicPublishTime: Date.now(), - topicPopularity: 100, - topicTags: ['啊这可海星', '啊这可海星'], - topicUpvotes: 1007, - topicViews: 10007, - topicLikes: 1007, - topicDislikes: 17, - topicPartition: ['galgame', '技术交流'], - topicReplies: { - reply: [ - // 存放了所有帖子的回复 - { - replyId: 1, - replier: { - avatar: './assets/images/topic.jpg', - name: '鲲鲲鲲', - cutenessPoints: 107, - }, - repliedTo: 'KUN', - isEdited: true, - editedTimestamp: Date.now(), - replyTime: Date.now(), - /* 此处过滤掉文本样式,评论只保留纯文本 */ - replyContent: '鲲鲲鲲', - replyUpvotes: 1, - replyLikes: 100, - replyDislikes: 10, - replyComments: [ - { - commentId: 1, - commenterAvatar: './assets/images/favicon.png', - commenterName: '啊这可海星', - commentedTo: 'azkhx', - commentUpvotes: 5, - commentDislikes: 1, - commentContent: '鲲鲲鲲鲲鲲,啊这可海星', - }, - { - commentId: 2, - commenterAvatar: './assets/images/favicon.png', - commenterName: '啊这可海星', - commentedTo: 'azkhx', - commentUpvotes: 5, - commentDislikes: 1, - commentContent: '鲲鲲鲲鲲鲲,啊这可海星', - }, - { - commentId: 3, - commenterAvatar: './assets/images/favicon.png', - commenterName: '啊这可海星', - commentedTo: 'azkhx', - commentUpvotes: 5, - commentDislikes: 1, - commentContent: '鲲鲲鲲鲲鲲,啊这可海星', - }, - ], - replyCount: 1, - }, - { - replyId: 2, - replier: { - avatar: './assets/images/topic.jpg', - name: 'KUN', - cutenessPoints: 107, - }, - repliedTo: 'KUN', - isEdited: true, - editedTimestamp: Date.now(), - replyTime: Date.now(), - replyContent: '鲲鲲鲲', - replyUpvotes: 1, - replyLikes: 100, - replyDislikes: 10, - replyComments: [ - { - commentId: 1, - commenterAvatar: './assets/images/favicon.png', - commenterName: '啊这可海星', - commentedTo: '鲲鲲鲲', - commentUpvotes: 5, - commentDislikes: 1, - /* 此处过滤掉文本样式,评论只保留纯文本 */ - commentContent: '鲲鲲鲲鲲鲲,啊这可海星', - }, - ], - replyCount: 1, - }, - ], - replyCount: 1, - }, - topicStatus: '正常', - topicIsEdited: false, - topicAuthor: { - avatar: './assets/images/KUN.jpg', - name: 'KUN', - cutenessPoints: 1007, - }, - }, -] +// 用户路由 +const userRoutes = require('./routes/user') -// 模拟用户数据 -const users = [ - { - registrationSequence: 1, - username: 'KUN', - email: 'kun@kungal.com', - token: 'KUNGalgame', - avatar: './assets/images/KUN.jpg', - registrationTime: Date.now(), - moemoepoint: 1007, - bio: '鲲最可爱!', - likesCount: 1007, - commentsCount: 50, - repliesCount: 20, - likedTopicsCount: 5, - repliedTopicsCount: 1007, - pushedTopicsCount: 3, - userTopics: { - likedTopicIds: [1, 2, 3], - repliedTopicIds: [4, 5], - pushedTopicIds: [6, 7, 8], - }, - publishedCommentsCount: 10, - publishedCommentIds: [1, 2, 3, 4, 5], - }, - { - registrationSequence: 2, - username: 'YUYU', - email: 'yuyu@kungal.com', - token: 'azkhx', - avatar: './assets/images/topic.jpg', - registrationTime: Date.now(), - cutePoints: 11000, - bio: '啊这可海星', - likesCount: 1007, - commentsCount: 20, - repliesCount: 10, - likedTopicsCount: 2, - repliedTopicsCount: 1007, - pushedTopicsCount: 1, - userTopics: { - likedTopicIds: [9, 10], - repliedTopicIds: [11], - pushedTopicIds: [12], - }, - publishedCommentsCount: 5, - publishedCommentIds: [6, 7, 8, 9, 10], - }, -] +// 帖子路由 +const topicRoutes = require('./routes/topic') -// 定义用户JSON数据的路由 -app.get('/kungalgamer/:id', (req, res) => { - const userId = parseInt(req.params.id) - const user = users.find((user) => user.registrationSequence === userId) +// 登录路由 +const loginRoutes = require('./routes/login') - if (!user) { - res.status(404).json({ error: 'User not found' }) - } else { - res.json(user) - } -}) +// 注册路由 +const registerRoutes = require('./routes/register') -// 获取指定帖子的路由 -app.get('/topic/:id', (req, res) => { - const topicId = parseInt(req.params.id) - const topic = topics.find((topic) => topic.topicId === topicId) - - if (topic) { - res.json(topic) - } else { - res.status(404).json({ error: 'Topic Not Found!' }) - } -}) - -// 获取帖子回复的路由 -app.get('/topic/reply/:id', (req, res) => { - const replyId = parseInt(req.params.id) - let targetReply - - for (const topic of topics) { - const reply = topic.topicReplies.reply.find( - (reply) => reply.replyId === replyId - ) - if (reply) { - targetReply = reply - break - } - } - - if (targetReply) { - res.json(targetReply) - } else { - res.status(404).json({ error: 'Reply not found!' }) - } -}) - -// 获取评论的路由 -app.get('/topic/reply/comment/:id', (req, res) => { - const replyId = Number(req.params.id) - - // 在帖子数据中查找指定回复 - const reply = topics - .flatMap((topic) => topic.topicReplies.reply) - .find((reply) => reply.replyId === replyId) - - if (!reply) { - res.status(404).json({ error: 'Comment not found' }) - } else { - res.json(reply.replyComments) - } -}) - -// 登录 -app.post('/api/login', (req, res) => { - const { email, password } = req.body - - // 登录逻辑 - if (email === 'kun@kungal.com' && password === '1007') { - // 登陆成功 - const token = 'KUNGalgame' - res.json({ success: true, token }) - } else { - // 登陆失败 - res.status(401).json({ success: false, error: 'Invalid email or password' }) - } -}) - -// 注册 -app.post('/api/register', (req, res) => { - // 获取请求 body 内容 - const { username, email, password, verificationCode } = req.body - - // 注册逻辑 - if (username && email && password && verificationCode) { - // 注册成功 - res.json({ success: true }) - } else { - // 注册失败 - res - .status(400) - .json({ success: false, error: 'Incomplete registration information' }) - } -}) +app.use('/kungalgamer', userRoutes) +app.use('/topic', topicRoutes) +app.use('/login', loginRoutes) +app.use('/register', registerRoutes) app.use((err, req, res, next) => { console.error(err.stack) diff --git a/server/routes/login.js b/server/routes/login.js new file mode 100644 index 00000000..c96dc14f --- /dev/null +++ b/server/routes/login.js @@ -0,0 +1,21 @@ +// login.js + +const express = require('express') +const router = express.Router() + +// 登录 +router.post('/api/login', (req, res) => { + const { email, password } = req.body + + // 登录逻辑 + if (email === 'kun@kungal.com' && password === '1007') { + // 登陆成功 + const token = 'KUNGalgame' + res.json({ success: true, token }) + } else { + // 登陆失败 + res.status(401).json({ success: false, error: 'Invalid email or password' }) + } +}) + +module.exports = router diff --git a/server/routes/register.js b/server/routes/register.js new file mode 100644 index 00000000..6a564691 --- /dev/null +++ b/server/routes/register.js @@ -0,0 +1,23 @@ +// register.js + +const express = require('express') +const router = express.Router() + +// 注册 +router.post('/api/register', (req, res) => { + // 获取请求 body 内容 + const { username, email, password, verificationCode } = req.body + + // 注册逻辑 + if (username && email && password && verificationCode) { + // 注册成功 + res.json({ success: true }) + } else { + // 注册失败 + res + .status(400) + .json({ success: false, error: 'Incomplete registration information' }) + } +}) + +module.exports = router diff --git a/server/routes/topic.js b/server/routes/topic.js new file mode 100644 index 00000000..45153997 --- /dev/null +++ b/server/routes/topic.js @@ -0,0 +1,267 @@ +// topic.js + +const express = require('express') +const router = express.Router() + +// 模拟一个 topics 数组 +const topics = [ + // 帖子的数组 + { + topicId: 1, + topicTitle: '啊这可海星啊这可海星啊这可海星啊这可海星啊这可海星', + topicFloorCount: 1, + topicContent: '

啊这可海星

', + topicPublishTime: Date.now(), + topicPopularity: 100, + topicTags: ['啊这可海星', '啊这可海星'], + topicUpvotes: 1007, + topicViews: 10007, + topicLikes: 1007, + topicDislikes: 17, + topicPartition: ['galgame'], + topicReplies: { + reply: [ + // 存放了所有帖子的回复 + { + replyId: 1, + replier: { + avatar: './assets/images/topic.jpg', + name: '鲲鲲鲲', + cutenessPoints: 107, + }, + repliedTo: 'KUN', + isEdited: true, + editedTimestamp: Date.now(), + replyTime: Date.now(), + /* 此处过滤掉文本样式,评论只保留纯文本 */ + replyContent: '鲲鲲鲲', + replyUpvotes: 1, + replyLikes: 100, + replyDislikes: 10, + replyComments: [ + { + commentId: 1, + commenterAvatar: './assets/images/favicon.png', + commenterName: '啊这可海星', + commentedTo: 'azkhx', + commentUpvotes: 5, + commentDislikes: 1, + commentContent: '鲲鲲鲲鲲鲲,啊这可海星', + }, + { + commentId: 2, + commenterAvatar: './assets/images/favicon.png', + commenterName: '啊这可海星', + commentedTo: 'azkhx', + commentUpvotes: 5, + commentDislikes: 1, + commentContent: '鲲鲲鲲鲲鲲,啊这可海星', + }, + { + commentId: 3, + commenterAvatar: './assets/images/favicon.png', + commenterName: '啊这可海星', + commentedTo: 'azkhx', + commentUpvotes: 5, + commentDislikes: 1, + commentContent: '鲲鲲鲲鲲鲲,啊这可海星', + }, + ], + replyCount: 1, + }, + { + replyId: 2, + replier: { + avatar: './assets/images/topic.jpg', + name: 'KUN', + cutenessPoints: 107, + }, + repliedTo: 'KUN', + isEdited: true, + editedTimestamp: Date.now(), + replyTime: Date.now(), + replyContent: '鲲鲲鲲', + replyUpvotes: 1, + replyLikes: 100, + replyDislikes: 10, + replyComments: [ + { + commentId: 1, + commenterAvatar: './assets/images/favicon.png', + commenterName: '啊这可海星', + commentedTo: '鲲鲲鲲', + commentUpvotes: 5, + commentDislikes: 1, + /* 此处过滤掉文本样式,评论只保留纯文本 */ + commentContent: '鲲鲲鲲鲲鲲,啊这可海星', + }, + ], + replyCount: 1, + }, + ], + replyCount: 1, + }, + topicStatus: '正常', + topicIsEdited: false, + topicAuthor: { + avatar: './assets/images/KUN.jpg', + name: 'KUN', + cutenessPoints: 1007, + }, + }, + { + topicId: 2, + topicTitle: '啊这可海星啊这可海星啊这可海星啊这可海星啊这可海星', + topicFloorCount: 1, + topicContent: '

啊这可海星

', + topicPublishTime: Date.now(), + topicPopularity: 100, + topicTags: ['啊这可海星', '啊这可海星'], + topicUpvotes: 1007, + topicViews: 10007, + topicLikes: 1007, + topicDislikes: 17, + topicPartition: ['galgame', '技术交流'], + topicReplies: { + reply: [ + // 存放了所有帖子的回复 + { + replyId: 1, + replier: { + avatar: './assets/images/topic.jpg', + name: '鲲鲲鲲', + cutenessPoints: 107, + }, + repliedTo: 'KUN', + isEdited: true, + editedTimestamp: Date.now(), + replyTime: Date.now(), + /* 此处过滤掉文本样式,评论只保留纯文本 */ + replyContent: '鲲鲲鲲', + replyUpvotes: 1, + replyLikes: 100, + replyDislikes: 10, + replyComments: [ + { + commentId: 1, + commenterAvatar: './assets/images/favicon.png', + commenterName: '啊这可海星', + commentedTo: 'azkhx', + commentUpvotes: 5, + commentDislikes: 1, + commentContent: '鲲鲲鲲鲲鲲,啊这可海星', + }, + { + commentId: 2, + commenterAvatar: './assets/images/favicon.png', + commenterName: '啊这可海星', + commentedTo: 'azkhx', + commentUpvotes: 5, + commentDislikes: 1, + commentContent: '鲲鲲鲲鲲鲲,啊这可海星', + }, + { + commentId: 3, + commenterAvatar: './assets/images/favicon.png', + commenterName: '啊这可海星', + commentedTo: 'azkhx', + commentUpvotes: 5, + commentDislikes: 1, + commentContent: '鲲鲲鲲鲲鲲,啊这可海星', + }, + ], + replyCount: 1, + }, + { + replyId: 2, + replier: { + avatar: './assets/images/topic.jpg', + name: 'KUN', + cutenessPoints: 107, + }, + repliedTo: 'KUN', + isEdited: true, + editedTimestamp: Date.now(), + replyTime: Date.now(), + replyContent: '鲲鲲鲲', + replyUpvotes: 1, + replyLikes: 100, + replyDislikes: 10, + replyComments: [ + { + commentId: 1, + commenterAvatar: './assets/images/favicon.png', + commenterName: '啊这可海星', + commentedTo: '鲲鲲鲲', + commentUpvotes: 5, + commentDislikes: 1, + /* 此处过滤掉文本样式,评论只保留纯文本 */ + commentContent: '鲲鲲鲲鲲鲲,啊这可海星', + }, + ], + replyCount: 1, + }, + ], + replyCount: 1, + }, + topicStatus: '正常', + topicIsEdited: false, + topicAuthor: { + avatar: './assets/images/KUN.jpg', + name: 'KUN', + cutenessPoints: 1007, + }, + }, +] + +// 获取指定帖子的路由 +router.get('/:id', (req, res) => { + const topicId = parseInt(req.params.id) + const topic = topics.find((topic) => topic.topicId === topicId) + + if (topic) { + res.json(topic) + } else { + res.status(404).json({ error: 'Topic Not Found!' }) + } +}) + +// 获取帖子回复的路由 +router.get('/reply/:id', (req, res) => { + const replyId = parseInt(req.params.id) + let targetReply + + for (const topic of topics) { + const reply = topic.topicReplies.reply.find( + (reply) => reply.replyId === replyId + ) + if (reply) { + targetReply = reply + break + } + } + + if (targetReply) { + res.json(targetReply) + } else { + res.status(404).json({ error: 'Reply not found!' }) + } +}) + +// 获取评论的路由 +router.get('/reply/comment/:id', (req, res) => { + const replyId = Number(req.params.id) + + // 在帖子数据中查找指定回复 + const reply = topics + .flatMap((topic) => topic.topicReplies.reply) + .find((reply) => reply.replyId === replyId) + + if (!reply) { + res.status(404).json({ error: 'Comment not found' }) + } else { + res.json(reply.replyComments) + } +}) + +module.exports = router diff --git a/server/routes/user.js b/server/routes/user.js new file mode 100644 index 00000000..2d1243b8 --- /dev/null +++ b/server/routes/user.js @@ -0,0 +1,68 @@ +// user.js + +const express = require('express') +const router = express.Router() + +// 模拟用户数据 +const users = [ + { + registrationSequence: 1, + username: 'KUN', + email: 'kun@kungal.com', + token: 'KUNGalgame', + avatar: './assets/images/KUN.jpg', + registrationTime: Date.now(), + moemoepoint: 1007, + bio: '鲲最可爱!', + likesCount: 1007, + commentsCount: 50, + repliesCount: 20, + likedTopicsCount: 5, + repliedTopicsCount: 1007, + pushedTopicsCount: 3, + userTopics: { + likedTopicIds: [1, 2, 3], + repliedTopicIds: [4, 5], + pushedTopicIds: [6, 7, 8], + }, + publishedCommentsCount: 10, + publishedCommentIds: [1, 2, 3, 4, 5], + }, + { + registrationSequence: 2, + username: 'YUYU', + email: 'yuyu@kungal.com', + token: 'azkhx', + avatar: './assets/images/topic.jpg', + registrationTime: Date.now(), + cutePoints: 11000, + bio: '啊这可海星', + likesCount: 1007, + commentsCount: 20, + repliesCount: 10, + likedTopicsCount: 2, + repliedTopicsCount: 1007, + pushedTopicsCount: 1, + userTopics: { + likedTopicIds: [9, 10], + repliedTopicIds: [11], + pushedTopicIds: [12], + }, + publishedCommentsCount: 5, + publishedCommentIds: [6, 7, 8, 9, 10], + }, +] + +// 定义用户JSON数据的路由 +router.get('/:id', (req, res) => { + const userId = parseInt(req.params.id) + const user = users.find((user) => user.registrationSequence === userId) + + if (!user) { + res.status(404).json({ error: 'User not found' }) + } else { + res.json(user) + } +}) + +module.exports = router