feat: 新增 nodejs 实验 8
This commit is contained in:
2
Server/tests/test8/.gitignore
vendored
Normal file
2
Server/tests/test8/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
node_modules
|
||||
pnpm-lock.yaml
|
||||
300
Server/tests/test8/Server.apifox.json
Normal file
300
Server/tests/test8/Server.apifox.json
Normal file
@@ -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": []
|
||||
}
|
||||
113
Server/tests/test8/Server.postman.json
Normal file
113
Server/tests/test8/Server.postman.json
Normal file
@@ -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": {}
|
||||
}
|
||||
22
Server/tests/test8/app.js
Normal file
22
Server/tests/test8/app.js
Normal file
@@ -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}`);
|
||||
})
|
||||
24
Server/tests/test8/db.js
Normal file
24
Server/tests/test8/db.js
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
20
Server/tests/test8/package.json
Normal file
20
Server/tests/test8/package.json
Normal file
@@ -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"
|
||||
}
|
||||
}
|
||||
41
Server/tests/test8/routes/index.js
Normal file
41
Server/tests/test8/routes/index.js
Normal file
@@ -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
|
||||
}
|
||||
13
Server/tests/test8/routes/routes.js
Normal file
13
Server/tests/test8/routes/routes.js
Normal file
@@ -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;
|
||||
Reference in New Issue
Block a user