Skip to content

Commit b7ce5d9

Browse files
authored
Merge pull request #3 from martinwq997/feature/api
api
2 parents 77f8fdf + 8e84ebd commit b7ce5d9

File tree

4 files changed

+389
-2558
lines changed

4 files changed

+389
-2558
lines changed

app/api/crates/route.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
// /home/rust/workspace/cratespro-frontend/app/api/programs/route.ts
21
import { NextRequest, NextResponse } from 'next/server';
3-
import pool from '../../lib/db';
42

53
export async function GET(req: NextRequest) {
64
try {
7-
const client = await pool.connect();
8-
const res = await client.query('SELECT name, description FROM programs');
9-
client.release();
5+
// 发送 HTTP 请求获取外部数据
6+
const externalApiUrl = 'http://210.28.134.203:6888/crates'; // 替换为你的外部 API URL
7+
const externalRes = await fetch(externalApiUrl);
108

11-
const programs = res.rows;
9+
if (!externalRes.ok) {
10+
throw new Error('Failed to fetch external data');
11+
}
1212

13-
return NextResponse.json(programs);
13+
const externalData = await externalRes.json();
14+
15+
return NextResponse.json(externalData);
1416
} catch (error) {
15-
console.error('Database query error:', error);
17+
console.error('Error:', error);
1618
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
1719
}
1820
}

0 commit comments

Comments
 (0)