1
- import express from ' express' ;
2
- import cors from ' cors' ;
3
- import routes from ' ./routes' ;
1
+ import express , { Express } from " express" ;
2
+ import cors from " cors" ;
3
+ import routes from " ./routes" ;
4
4
5
5
// 扩展全局对象类型
6
6
declare global {
7
7
var verboseLogging : boolean ;
8
8
}
9
9
10
- const app = express ( ) ;
10
+ const app : Express = express ( ) ;
11
11
12
12
// Middleware
13
13
app . use ( cors ( ) ) ;
14
- app . use ( express . json ( { limit : ' 10mb' } ) ) ;
14
+ app . use ( express . json ( { limit : " 10mb" } ) ) ;
15
15
app . use ( express . urlencoded ( { extended : true } ) ) ;
16
16
17
17
// Request logging middleware (conditional)
@@ -21,29 +21,29 @@ app.use((req, res, next) => {
21
21
console . log ( `Original URL: ${ req . originalUrl } ` ) ;
22
22
console . log ( `Base URL: ${ req . baseUrl } ` ) ;
23
23
if ( req . body && Object . keys ( req . body ) . length > 0 ) {
24
- console . log ( ' Request body:' , JSON . stringify ( req . body , null , 2 ) ) ;
24
+ console . log ( " Request body:" , JSON . stringify ( req . body , null , 2 ) ) ;
25
25
}
26
26
}
27
27
next ( ) ;
28
28
} ) ;
29
29
30
30
// Add a simple root endpoint for debugging
31
- app . get ( '/' , ( req , res ) => {
31
+ app . get ( "/" , ( req , res ) => {
32
32
res . json ( {
33
- message : ' Mock OpenAI API Server' ,
34
- status : ' running' ,
33
+ message : " Mock OpenAI API Server" ,
34
+ status : " running" ,
35
35
timestamp : new Date ( ) . toISOString ( ) ,
36
36
availableEndpoints : [
37
- ' GET /health' ,
38
- ' GET /v1/models' ,
39
- ' POST /v1/chat/completions' ,
40
- ' POST /v1/images/generations'
41
- ]
37
+ " GET /health" ,
38
+ " GET /v1/models" ,
39
+ " POST /v1/chat/completions" ,
40
+ " POST /v1/images/generations" ,
41
+ ] ,
42
42
} ) ;
43
43
} ) ;
44
44
45
45
// Routes
46
- app . use ( '/' , routes ) ;
46
+ app . use ( "/" , routes ) ;
47
47
48
48
// 404 handler
49
49
app . use ( ( req , res ) => {
@@ -55,25 +55,32 @@ app.use((req, res) => {
55
55
res . status ( 404 ) . json ( {
56
56
error : {
57
57
message : `Path not found: ${ req . originalUrl } ` ,
58
- type : ' not_found_error' ,
59
- code : ' path_not_found' ,
58
+ type : " not_found_error" ,
59
+ code : " path_not_found" ,
60
60
method : req . method ,
61
61
path : req . path ,
62
- originalUrl : req . originalUrl
63
- }
62
+ originalUrl : req . originalUrl ,
63
+ } ,
64
64
} ) ;
65
65
} ) ;
66
66
67
67
// Global error handler
68
- app . use ( ( err : any , req : express . Request , res : express . Response , next : express . NextFunction ) => {
69
- console . error ( 'Global error:' , err ) ;
70
- res . status ( 500 ) . json ( {
71
- error : {
72
- message : 'Internal server error' ,
73
- type : 'api_error' ,
74
- code : 'internal_error'
75
- }
76
- } ) ;
77
- } ) ;
68
+ app . use (
69
+ (
70
+ err : any ,
71
+ req : express . Request ,
72
+ res : express . Response ,
73
+ next : express . NextFunction
74
+ ) => {
75
+ console . error ( "Global error:" , err ) ;
76
+ res . status ( 500 ) . json ( {
77
+ error : {
78
+ message : "Internal server error" ,
79
+ type : "api_error" ,
80
+ code : "internal_error" ,
81
+ } ,
82
+ } ) ;
83
+ }
84
+ ) ;
78
85
79
- export default app ;
86
+ export default app ;
0 commit comments