完成deno后端测试用的接口

This commit is contained in:
编码猿
2025-09-16 23:40:58 +08:00
parent c156637f03
commit 61c84b0842
12 changed files with 1576 additions and 9 deletions

13
back-end/.gitignore vendored Normal file
View File

@@ -0,0 +1,13 @@
# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local
# Fresh build directory
_fresh/
# npm + other dependencies
node_modules/
vendor/

17
back-end/README.md Normal file
View File

@@ -0,0 +1,17 @@
# Fresh project
Your new Fresh project is ready to go. You can follow the Fresh "Getting
Started" guide here: https://fresh.deno.dev/docs/getting-started
### Usage
Make sure to install Deno:
https://docs.deno.com/runtime/getting_started/installation
Then start the project in development mode:
```
deno task dev
```
This will watch the project directory and restart as necessary.

144
back-end/assets/styles.css Normal file
View File

@@ -0,0 +1,144 @@
*,
*::before,
*::after {
box-sizing: border-box;
}
* {
margin: 0;
}
button {
color: inherit;
}
button, [role="button"] {
cursor: pointer;
}
code {
font-family:
ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono",
"Courier New", monospace;
font-size: 1em;
}
img,
svg {
display: block;
}
img,
video {
max-width: 100%;
height: auto;
}
html {
line-height: 1.5;
-webkit-text-size-adjust: 100%;
font-family:
ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif,
"Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol",
"Noto Color Emoji";
}
.transition-colors {
transition-property: background-color, border-color, color, fill, stroke;
transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
transition-duration: 150ms;
}
.my-6 {
margin-bottom: 1.5rem;
margin-top: 1.5rem;
}
.text-4xl {
font-size: 2.25rem;
line-height: 2.5rem;
}
.mx-2 {
margin-left: 0.5rem;
margin-right: 0.5rem;
}
.my-4 {
margin-bottom: 1rem;
margin-top: 1rem;
}
.mx-auto {
margin-left: auto;
margin-right: auto;
}
.px-4 {
padding-left: 1rem;
padding-right: 1rem;
}
.py-8 {
padding-bottom: 2rem;
padding-top: 2rem;
}
.bg-\[\#86efac\] {
background-color: #86efac;
}
.text-3xl {
font-size: 1.875rem;
line-height: 2.25rem;
}
.py-6 {
padding-bottom: 1.5rem;
padding-top: 1.5rem;
}
.px-2 {
padding-left: 0.5rem;
padding-right: 0.5rem;
}
.py-1 {
padding-bottom: 0.25rem;
padding-top: 0.25rem;
}
.border-gray-500 {
border-color: #6b7280;
}
.bg-white {
background-color: #fff;
}
.flex {
display: flex;
}
.gap-8 {
grid-gap: 2rem;
gap: 2rem;
}
.font-bold {
font-weight: 700;
}
.max-w-screen-md {
max-width: 768px;
}
.flex-col {
flex-direction: column;
}
.items-center {
align-items: center;
}
.justify-center {
justify-content: center;
}
.border-2 {
border-width: 2px;
}
.rounded-sm {
border-radius: 0.25rem;
}
.hover\:bg-gray-200:hover {
background-color: #e5e7eb;
}
.tabular-nums {
font-variant-numeric: tabular-nums;
}
.min-h-screen {
min-height: 100vh;
}
.fresh-gradient {
background-color: rgb(134, 239, 172);
background-image: linear-gradient(
to right bottom,
rgb(219, 234, 254),
rgb(187, 247, 208),
rgb(254, 249, 195)
);
}

2
back-end/client.ts Normal file
View File

@@ -0,0 +1,2 @@
// Import CSS files here for hot module reloading to work.
import "./assets/styles.css";

View File

@@ -1,8 +1,53 @@
{
"nodeModulesDir": "auto",
"tasks": {
"dev": "deno run --watch main.ts"
"check": "deno fmt --check . && deno lint . && deno check",
"dev": "vite",
"build": "vite build",
"start": "deno serve -A _fresh/server.js",
"update": "deno run -A -r jsr:@fresh/update ."
},
"lint": {
"rules": {
"tags": [
"fresh",
"recommended"
]
}
},
"exclude": [
"**/_fresh/*"
],
"imports": {
"@std/assert": "jsr:@std/assert@1"
"fresh": "jsr:@fresh/core@^2.1.0",
"@fresh/plugin-vite": "jsr:@fresh/plugin-vite@^1.0.3",
"vite": "npm:vite@^7.1.3"
},
"compilerOptions": {
"lib": [
"dom",
"dom.asynciterable",
"dom.iterable",
"deno.ns"
],
"jsxPrecompileSkipElements": [
"a",
"img",
"source",
"body",
"html",
"head",
"title",
"meta",
"script",
"link",
"style",
"base",
"noscript",
"template"
],
"types": [
"vite/client"
]
}
}

1231
back-end/deno.lock generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,7 +1,82 @@
export function add(a: number, b: number): number {
return a + b;
import { App, staticFiles, cors } from "fresh";
import { define, type State } from "./utils.ts";
export const app = new App<State>();
app.use(cors({
origin: "*",
allowHeaders: ["*"],
allowMethods: ["POST", "GET", "OPTIONS"],
exposeHeaders: ["Content-Length", "X-Kuma-Revision"],
maxAge: 600,
credentials: true,
}))
app.use(staticFiles());
// 模拟产品数据
const mockProductsList = [
{
id: 1,
name: "Xiaomi Buds 5 Pro",
description: "高音质无线蓝牙耳机续航可达30小时",
price: 1199.19,
imageUrl: "https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1740638541.0557913.png",
stock: 2000,
categoryId: 1,
},
{
id: 2,
name: "机械键盘MK71pro",
description: "青轴机械键盘,全键无冲",
price: 899.00,
imageUrl: "https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/67D4F43CD4C647AD8F45892D6567E14A.png",
stock: 1000,
categoryId: 1,
},
{
id: 3,
name: "Xiaomi Watch S4 Sport",
description: "多功能智能手表支持心率监测和GPS",
price: 1699.00,
imageUrl: "https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1721233615.78223053.png",
stock: 200,
categoryId: 1,
}
];
const Res = <T extends Array<Object> | Object>(data: T) => {
return JSON.stringify({
code: 200,
message: "success",
data: data
});
}
if (import.meta.main) {
console.log("Add 2 + 3 =", add(2, 3));
}
// http://127.0.0.1:3000/api/productsList
app.get("/api/productsList", (ctx: { params: { name: any; }; }) => {
return new Response(Res(mockProductsList));
});
// http://127.0.0.1:3000/api/productsInfo?id=3
app.get("/api/productsInfo", (req: Request, ctx: { params: { id: number; }; }) => {
const id = (new URL(req.url)).searchParams.get('id')
if (!id) {
return new Response(Res("缺少id参数"));
}
const result = mockProductsList.find(product => product.id == Number(id))
if (!result) {
return new Response(Res("找不到数据"));
}
return new Response(Res(result));
});
const exampleLoggerMiddleware = define.middleware((ctx: { req: { method: any; url: any; }; next: () => any; }) => {
console.log(`${ctx.req.method} ${ctx.req.url}`);
return ctx.next();
});
app.use(exampleLoggerMiddleware);

BIN
back-end/static/favicon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

19
back-end/static/logo.svg Normal file
View File

@@ -0,0 +1,19 @@
<svg width="40" height="40" fill="none" xmlns="http://www.w3.org/2000/svg">
<path
d="M34.092 8.845C38.929 20.652 34.092 27 30 30.5c1 3.5-2.986 4.222-4.5 2.5-4.457 1.537-13.512 1.487-20-5C2 24.5 4.73 16.714 14 11.5c8-4.5 16-7 20.092-2.655Z"
fill="#FFDB1E"
/>
<path
d="M14 11.5c6.848-4.497 15.025-6.38 18.368-3.47C37.5 12.5 21.5 22.612 15.5 25c-6.5 2.587-3 8.5-6.5 8.5-3 0-2.5-4-5.183-7.75C2.232 23.535 6.16 16.648 14 11.5Z"
fill="#fff"
stroke="#FFDB1E"
/>
<path
d="M28.535 8.772c4.645 1.25-.365 5.695-4.303 8.536-3.732 2.692-6.606 4.21-7.923 4.83-.366.173-1.617-2.252-1.617-1 0 .417-.7 2.238-.934 2.326-1.365.512-4.223 1.29-5.835 1.29-3.491 0-1.923-4.754 3.014-9.122.892-.789 1.478-.645 2.283-.645-.537-.773-.534-.917.403-1.546C17.79 10.64 23 8.77 25.212 8.42c.366.014.82.35.82.629.41-.14 2.095-.388 2.503-.278Z"
fill="#FFE600"
/>
<path
d="M14.297 16.49c.985-.747 1.644-1.01 2.099-2.526.566.121.841-.08 1.29-.701.324.466 1.657.608 2.453.701-.715.451-1.057.852-1.452 2.106-1.464-.611-3.167-.302-4.39.42Z"
fill="#fff"
/>
</svg>

After

Width:  |  Height:  |  Size: 1.1 KiB

9
back-end/utils.ts Normal file
View File

@@ -0,0 +1,9 @@
import { createDefine } from "fresh";
// This specifies the type of "ctx.state" which is used to share
// data among middlewares, layouts and routes.
export interface State {
shared: string;
}
export const define = createDefine<State>();

12
back-end/vite.config.ts Normal file
View File

@@ -0,0 +1,12 @@
import { defineConfig } from "vite";
import { fresh } from "@fresh/plugin-vite";
export default defineConfig({
server: {
port: 3000, // 设置Vite开发服务器端口
strictPort: true, // 严格使用指定端口,如果被占用则报错
// 可选:允许跨域请求
cors: true,
},
plugins: [fresh()],
});

View File

@@ -1,5 +1,5 @@
export default class HttpConfig {
public static DevBaseUrl: string = "http://127.0.0.1:8080"
public static prodBaseUrl: string = "http://127.0.0.1:9090/api"
public static DevBaseUrl: string = "http://127.0.0.1:3000/api"
public static prodBaseUrl: string = "http://127.0.0.1:9090/api/"
public static apiList: string = ""
}