Skip to content

Commit 053df33

Browse files
committed
前后端连接
1 parent 8e84ebd commit 053df33

File tree

6 files changed

+5502
-1963
lines changed

6 files changed

+5502
-1963
lines changed

app/api/crates/route.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@ import { NextRequest, NextResponse } from 'next/server';
33
export async function GET(req: NextRequest) {
44
try {
55
// 发送 HTTP 请求获取外部数据
6-
const externalApiUrl = 'http://210.28.134.203:6888/crates'; // 替换为你的外部 API URL
6+
const externalApiUrl = 'http://210.28.134.203:6888/api/crates'; // 替换为你的外部 API URL
77
const externalRes = await fetch(externalApiUrl);
8-
98
if (!externalRes.ok) {
109
throw new Error('Failed to fetch external data');
1110
}
12-
1311
const externalData = await externalRes.json();
1412

1513
return NextResponse.json(externalData);
@@ -18,3 +16,4 @@ export async function GET(req: NextRequest) {
1816
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
1917
}
2018
}
19+

app/api/submit/route.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
import { NextResponse } from 'next/server';
22

3-
export async function POST(request: { json: () => any; }) {
4-
const apiUrl = process.env.API_URL; // 读取环境变量,获取后端服务的基础 URL
5-
const body = await request.json(); // 解析请求体
3+
export async function POST(request: Request) {
4+
const apiUrl = process.env.API_URL; // 读取环境变量
5+
const formData = await request.formData(); // 解析请求体
6+
7+
console.log("Request FormData:", formData);
68

79
try {
8-
const response = await fetch(`${apiUrl}/submit`, { //向后端发送服务请求
10+
const response = await fetch(`${apiUrl}`, {
911
method: 'POST',
10-
headers: {
11-
'Content-Type': 'application/json',
12-
},
13-
body: JSON.stringify(body), // 将请求体发送到后端
12+
body: formData, // 保持 FormData
1413
});
15-
//请求失败直接返回
14+
1615
if (!response.ok) {
17-
return NextResponse.json({ error: 'Failed to submit data' }, { status: 500 });
16+
return NextResponse.json({ error: 'Failed to submit data' }, { status: response.status });
1817
}
19-
//解析成功的响应
20-
const result = await response.json();
18+
19+
const result = await response.json(); // 确认返回的是 JSON 格式
2120
return NextResponse.json({ message: 'Submission successful', data: result });
2221
} catch (error) {
2322
return NextResponse.json({ error: 'An error occurred while submitting data.' }, { status: 500 });

app/programs/page.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,21 @@ interface ObjectData {
1313
interface CVE {
1414
id: string;
1515
description: string;
16+
1617
name: string;
1718
version: string;
1819
}
1920

2021
const ProgramsPage = () => {
22+
23+
24+
2125
const [objects, setObjects] = useState<ObjectData[]>([]); //从接口获取Rust程序数据
2226
const [cveData, setCveData] = useState<CVE[]>([]); //从接口获取CVE数据
2327

2428
//获取数据
2529
useEffect(() => {
26-
fetch('/api/crates') //这里应调用后端接口:GET http://localhost:6888/api/crates (?)
30+
fetch('/api/crates') //这里应调用nextjs路由
2731
.then(response => response.json())
2832
.then(data => {
2933
console.log('Fetched crates data:', data); //调试输出
@@ -35,15 +39,15 @@ const ProgramsPage = () => {
3539
}
3640
});
3741

38-
fetch('/api/cves') //这里应调用后端接口: GET http://localhost:6888/api/crates/{name} (?)
42+
fetch('/api/cves') //这里应调用nextjs路由
3943
.then(response => {
4044
if (!response.ok) {
4145
throw new Error(`HTTP error! status: ${response.status}`);
4246
}
4347
return response.text(); // 暂时使用 text() 来调试
4448
})
4549
.then(text => {
46-
console.log('Response text:', text);
50+
//console.log('Response text:', text);
4751
return JSON.parse(text); // 手动解析 JSON
4852
})
4953
.then(data => setCveData(data))

app/ui/programs/nav-links.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,24 @@ const NavLinks: React.FC = () => {
3636
//暂定上传数据类型为react表单类型
3737
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
3838
e.preventDefault();
39-
const formData = new FormData();
40-
if (isGithubLink) {
41-
formData.append('githubLink', inputValue);
42-
} else if (file) {
43-
formData.append('file', file);
44-
}
39+
4540

4641
try {
42+
const formData = new FormData();
43+
if (isGithubLink) {
44+
formData.append('githubLink', inputValue);
45+
} else if (file) {
46+
formData.append('file', file);
47+
}
48+
4749
//用fetch向服务器发声POST请求,提交用户输入的内容
4850
const response = await fetch('/api/submit', { //待替换为后端服务API
4951
method: 'POST',
5052
//请求体
5153
body: formData,
5254
});
53-
//console.log('Response:', response); // 输出响应
55+
console.log("aaaaaaaaaaaaaaaaaadata ", formData);
56+
console.log('Response:', response); // 输出响应
5457

5558
//响应处理,根据响应结果显示提示信息,并重置输入框或关闭弹窗
5659
if (response.ok) {

0 commit comments

Comments
 (0)