From ad1a6a22e9bd8ddd420b888ea96c94dc86c4fb17 Mon Sep 17 00:00:00 2001 From: zhilv Date: Tue, 9 Dec 2025 11:08:15 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=20nodejs=20=E5=AE=9E?= =?UTF-8?q?=E9=AA=8C=208?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Server/tests/test8/.gitignore | 2 + Server/tests/test8/Server.apifox.json | 300 +++++++++++++++++++++++++ Server/tests/test8/Server.postman.json | 113 ++++++++++ Server/tests/test8/app.js | 22 ++ Server/tests/test8/db.js | 24 ++ Server/tests/test8/package.json | 20 ++ Server/tests/test8/routes/index.js | 41 ++++ Server/tests/test8/routes/routes.js | 13 ++ 8 files changed, 535 insertions(+) create mode 100644 Server/tests/test8/.gitignore create mode 100644 Server/tests/test8/Server.apifox.json create mode 100644 Server/tests/test8/Server.postman.json create mode 100644 Server/tests/test8/app.js create mode 100644 Server/tests/test8/db.js create mode 100644 Server/tests/test8/package.json create mode 100644 Server/tests/test8/routes/index.js create mode 100644 Server/tests/test8/routes/routes.js diff --git a/Server/tests/test8/.gitignore b/Server/tests/test8/.gitignore new file mode 100644 index 0000000..f52e6f5 --- /dev/null +++ b/Server/tests/test8/.gitignore @@ -0,0 +1,2 @@ +node_modules +pnpm-lock.yaml \ No newline at end of file diff --git a/Server/tests/test8/Server.apifox.json b/Server/tests/test8/Server.apifox.json new file mode 100644 index 0000000..03d6a1c --- /dev/null +++ b/Server/tests/test8/Server.apifox.json @@ -0,0 +1,300 @@ +{ + "apifoxProject": "1.0.0", + "$schema": { + "app": "apifox", + "type": "project", + "version": "1.2.0" + }, + "info": { + "name": "Server", + "description": "", + "mockRule": { + "rules": [], + "enableSystemRule": true + } + }, + "apiCollection": [ + { + "name": "根目录", + "id": 70829727, + "auth": {}, + "securityScheme": {}, + "parentId": 0, + "serverId": "", + "description": "", + "identityPattern": { + "httpApi": { + "type": "methodAndPath", + "bodyType": "", + "fields": [] + } + }, + "shareSettings": {}, + "visibility": "SHARED", + "moduleId": 6526371, + "preProcessors": [ + { + "id": "inheritProcessors", + "type": "inheritProcessors", + "data": {} + } + ], + "postProcessors": [ + { + "id": "inheritProcessors", + "type": "inheritProcessors", + "data": {} + } + ], + "inheritPostProcessors": {}, + "inheritPreProcessors": {}, + "items": [ + { + "name": "test8", + "id": 73260890, + "auth": {}, + "securityScheme": {}, + "parentId": 0, + "serverId": "", + "description": "", + "identityPattern": { + "httpApi": { + "type": "inherit", + "bodyType": "", + "fields": [] + } + }, + "shareSettings": {}, + "visibility": "INHERITED", + "moduleId": 6526371, + "preProcessors": [ + { + "id": "inheritProcessors", + "type": "inheritProcessors", + "data": {} + } + ], + "postProcessors": [ + { + "id": "inheritProcessors", + "type": "inheritProcessors", + "data": {} + } + ], + "inheritPostProcessors": {}, + "inheritPreProcessors": {}, + "items": [ + { + "name": "register", + "api": { + "id": "388297363", + "method": "post", + "path": "http://localhost:3000/register", + "parameters": {}, + "auth": {}, + "securityScheme": {}, + "commonParameters": { + "query": [], + "body": [], + "cookie": [], + "header": [] + }, + "responses": [ + { + "id": "125594353", + "code": 201, + "name": "成功", + "jsonSchema": { + "type": "object", + "properties": { + "message": { + "type": "string" + } + }, + "required": [ + "message" + ] + }, + "contentType": "json" + } + ], + "responseExamples": [], + "requestBody": { + "type": "application/json", + "parameters": [], + "jsonSchema": { + "type": "object", + "properties": {} + }, + "mediaType": "", + "examples": [ + { + "mediaType": "application/json", + "value": "{\r\n \"username\": \"abc\",\r\n \"password\": \"abc\",\r\n \"email\": \"abc@email.com\",\r\n \"gender\": \"male\",\r\n \"hobbies\": [\r\n \"听音乐\",\r\n \"玩游戏\"\r\n ],\r\n \"city\": \"重庆\",\r\n \"description\": \"爱坤坤\"\r\n}" + } + ], + "oasExtensions": "" + }, + "description": "", + "tags": [], + "status": "developing", + "serverId": "", + "operationId": "", + "sourceUrl": "", + "ordering": 10, + "cases": [], + "mocks": [], + "customApiFields": "{}", + "advancedSettings": { + "disabledSystemHeaders": {} + }, + "mockScript": {}, + "codeSamples": [], + "commonResponseStatus": {}, + "responseChildren": [], + "visibility": "INHERITED", + "moduleId": 6526371, + "oasExtensions": "", + "type": "http", + "preProcessors": [], + "postProcessors": [], + "inheritPostProcessors": {}, + "inheritPreProcessors": {} + } + }, + { + "name": "login", + "api": { + "id": "388297808", + "method": "post", + "path": "http://localhost:3000/login", + "parameters": {}, + "auth": {}, + "securityScheme": {}, + "commonParameters": { + "query": [], + "body": [], + "cookie": [], + "header": [] + }, + "responses": [ + { + "id": "103375879", + "code": 200, + "name": "成功", + "contentType": "raw" + } + ], + "responseExamples": [], + "requestBody": { + "type": "application/json", + "parameters": [], + "jsonSchema": { + "type": "object", + "properties": {} + }, + "mediaType": "", + "examples": [ + { + "mediaType": "application/json", + "value": "{\r\n \"username\": \"abc\",\r\n \"password\": \"abc\"\r\n}" + } + ], + "oasExtensions": "" + }, + "description": "", + "tags": [], + "status": "developing", + "serverId": "", + "operationId": "", + "sourceUrl": "", + "ordering": 20, + "cases": [], + "mocks": [], + "customApiFields": "{}", + "advancedSettings": { + "disabledSystemHeaders": {} + }, + "mockScript": {}, + "codeSamples": [], + "commonResponseStatus": {}, + "responseChildren": [], + "visibility": "INHERITED", + "moduleId": 6526371, + "oasExtensions": "", + "type": "http", + "preProcessors": [], + "postProcessors": [], + "inheritPostProcessors": {}, + "inheritPreProcessors": {} + } + } + ] + } + ] + } + ], + "socketCollection": [], + "docCollection": [], + "webSocketCollection": [], + "socketIOCollection": [], + "responseCollection": [], + "schemaCollection": [], + "securitySchemeCollection": [], + "requestCollection": [ + { + "name": "根目录", + "children": [], + "ordering": [ + "requestFolder.7895936" + ], + "items": [] + } + ], + "environments": [], + "commonScripts": [], + "globalVariables": [], + "commonParameters": null, + "projectSetting": { + "id": "7426031", + "auth": {}, + "securityScheme": {}, + "gateway": [], + "language": "zh-CN", + "apiStatuses": [ + "developing", + "testing", + "released", + "deprecated" + ], + "mockSettings": {}, + "preProcessors": [], + "postProcessors": [], + "advancedSettings": { + "enableJsonc": false, + "enableBigint": false, + "responseValidate": true, + "enableTestScenarioSetting": false, + "enableYAPICompatScript": false, + "isDefaultUrlEncoding": 2, + "publishedDocUrlRules": { + "defaultRule": "RESOURCE_KEY_ONLY", + "resourceKeyStandard": "NEW" + } + }, + "initialDisabledMockIds": [], + "servers": [ + { + "id": "default", + "name": "默认服务" + } + ], + "cloudMock": { + "security": "free", + "enable": false, + "tokenKey": "apifoxToken" + } + }, + "customFunctions": [], + "projectAssociations": [] +} \ No newline at end of file diff --git a/Server/tests/test8/Server.postman.json b/Server/tests/test8/Server.postman.json new file mode 100644 index 0000000..9f78744 --- /dev/null +++ b/Server/tests/test8/Server.postman.json @@ -0,0 +1,113 @@ +{ + "info": { + "name": "Server", + "description": "", + "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" + }, + "item": [ + { + "name": "test8", + "description": "", + "item": [ + { + "name": "register", + "description": "", + "event": [], + "auth": {}, + "request": { + "auth": {}, + "method": "POST", + "body": { + "mode": "raw", + "raw": "{\r\n \"username\": \"abc\",\r\n \"password\": \"abc\",\r\n \"email\": \"abc@email.com\",\r\n \"gender\": \"male\",\r\n \"hobbies\": [\r\n \"听音乐\",\r\n \"玩游戏\"\r\n ],\r\n \"city\": \"重庆\",\r\n \"description\": \"爱坤坤\"\r\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "header": [], + "url": { + "raw": "http://localhost:3000/register", + "path": [ + "register" + ], + "host": [ + "localhost" + ], + "protocol": "http", + "port": "3000", + "query": [], + "variable": [] + } + }, + "response": [], + "protocolProfileBehavior": { + "strictSSL": false, + "followRedirects": true + } + }, + { + "name": "login", + "description": "", + "event": [], + "auth": {}, + "request": { + "auth": {}, + "method": "POST", + "body": { + "mode": "raw", + "raw": "{\r\n \"username\": \"abc\",\r\n \"password\": \"abc\"\r\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "header": [], + "url": { + "raw": "http://localhost:3000/login", + "path": [ + "login" + ], + "host": [ + "localhost" + ], + "protocol": "http", + "port": "3000", + "query": [], + "variable": [] + } + }, + "response": [], + "protocolProfileBehavior": { + "strictSSL": false, + "followRedirects": true + } + } + ], + "event": [ + { + "listen": "prerequest", + "script": { + "exec": [], + "type": "text/javascript", + "packages": {} + } + }, + { + "listen": "test", + "script": { + "exec": [], + "type": "text/javascript", + "packages": {} + } + } + ], + "auth": {} + } + ], + "variable": [], + "event": [], + "auth": {} +} \ No newline at end of file diff --git a/Server/tests/test8/app.js b/Server/tests/test8/app.js new file mode 100644 index 0000000..1f37bb8 --- /dev/null +++ b/Server/tests/test8/app.js @@ -0,0 +1,22 @@ +const koa = require("koa"); +const bodyParser = require("koa-bodyparser"); +const path = require("path"); +const koaStatic = require("koa-static") + +const routes = require("./routes/routes"); +const indexRoutes = require("./routes/index"); +const db = require("./db") + +const app = new koa(); + +app.use(bodyParser()); + +app.use(routes.routes()); +app.use(routes.allowedMethods()); + +app.use(koaStatic(path.join(__dirname, "public"))); + +const port = 3000; +app.listen(port, () => { + console.log(`Server is running on http://localhost:${port}`); +}) diff --git a/Server/tests/test8/db.js b/Server/tests/test8/db.js new file mode 100644 index 0000000..0bba42b --- /dev/null +++ b/Server/tests/test8/db.js @@ -0,0 +1,24 @@ +const mysql = require("mysql2/promise") + +let pool; + +const connect = async () => { + if (!pool) { + pool = mysql.createPool({ + host: "localhost", + user: "root", + password: "123456", + database: "express_db" + }) + } + console.log("database is connect"); + return pool; +} + +module.exports = { + connect, + query: async (sql, values) => { + const pool = await connect(); + return pool.query(sql, values); + } +} \ No newline at end of file diff --git a/Server/tests/test8/package.json b/Server/tests/test8/package.json new file mode 100644 index 0000000..f3f9e39 --- /dev/null +++ b/Server/tests/test8/package.json @@ -0,0 +1,20 @@ +{ + "name": "test8", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "packageManager": "pnpm@10.14.0", + "dependencies": { + "koa": "^3.1.1", + "koa-bodyparser": "^4.4.1", + "koa-router": "^14.0.0", + "koa-static": "^5.0.0", + "mysql2": "^3.15.3" + } +} diff --git a/Server/tests/test8/routes/index.js b/Server/tests/test8/routes/index.js new file mode 100644 index 0000000..064db20 --- /dev/null +++ b/Server/tests/test8/routes/index.js @@ -0,0 +1,41 @@ +const db = require("../db"); + +const handlerLogin = async (ctx) => { + const { username, password } = ctx.request.body; + console.log(username + password); + + const [rows] = await db.query("select * from users where username = ? ", [username]); + if (rows.length > 0) { + const user = rows[0]; + console.log(user.password, password); + + if (user.password === password) { + ctx.body = "登录成功"; + } else { + ctx.body = "密码错误"; + } + } else { + ctx.body = "用户名不存在"; + } +} + +const handlerRegister = async (ctx) => { + const { username, password, email, gender, hobbies, city, description } = ctx.request.body; + try { + await db.query("insert into users(username, password, email, gender, hobbies, city, description) values(?, ?, ?, ?, ?, ?, ?)", [username, password, email, gender, JSON.stringify(hobbies), city, description]); + ctx.status = 201; + ctx.body = { + message: "注册成功" + }; + } catch (err) { + console.error(err); + ctx.status = 500; + ctx.body = { + error: "服务器内部错误, 请稍后重试" + }; + } +} + +module.exports = { + handlerLogin, handlerRegister +} \ No newline at end of file diff --git a/Server/tests/test8/routes/routes.js b/Server/tests/test8/routes/routes.js new file mode 100644 index 0000000..0846862 --- /dev/null +++ b/Server/tests/test8/routes/routes.js @@ -0,0 +1,13 @@ +const Router = require("koa-router"); + +const db = require("../db") +const { handlerLogin, handlerRegister } = require("./index"); + +const router = new Router(); + +router.post("/register", handlerRegister); +router.post("/login", handlerLogin); + +// router.get("/user/:id", handlerGetUser) + +module.exports = router; \ No newline at end of file