1
1
const express = require ( 'express' ) ;
2
2
const bodyParser = require ( 'body-parser' ) ;
3
3
const cors = require ( 'cors' ) ;
4
- const path = require ( 'path' ) ;
5
4
const swaggerUi = require ( 'swagger-ui-express' ) ;
5
+ const path = require ( 'path' ) ;
6
6
const openApiSpec = require ( '../swagger/openapi.json' ) ;
7
7
8
8
const app = express ( ) ;
9
- const PORT = process . env . PORT || 3000 ;
10
9
11
10
app . use ( cors ( ) ) ;
12
11
app . use ( bodyParser . json ( ) ) ;
@@ -20,16 +19,20 @@ app.use('/api/projects', projectRoutes);
20
19
app . use ( '/api/tasks' , taskRoutes ) ;
21
20
app . use ( '/api/users' , userRoutes ) ;
22
21
23
- // Swagger UI
22
+ // Serve Swagger UI
24
23
app . use ( '/api-docs' , swaggerUi . serve , swaggerUi . setup ( openApiSpec ) ) ;
25
24
26
- // OpenAPI JSON
25
+ // Serve the OpenAPI JSON
27
26
app . get ( '/openapi.json' , ( req , res ) => {
28
27
res . sendFile ( path . join ( __dirname , '../swagger/openapi.json' ) ) ;
29
28
} ) ;
30
29
31
- app . listen ( PORT , ( ) => {
32
- console . log ( `Server is running on http://localhost:${ PORT } ` ) ;
33
- console . log ( `API documentation available at http://localhost:${ PORT } /api-docs` ) ;
34
- console . log ( `OpenAPI JSON available at http://localhost:${ PORT } /openapi.json` ) ;
35
- } ) ;
30
+ // Export the app for Vercel or start the server locally
31
+ if ( process . env . NODE_ENV !== 'production' ) {
32
+ const PORT = process . env . PORT || 3000 ;
33
+ app . listen ( PORT , ( ) => {
34
+ console . log ( `Server is running locally at http://localhost:${ PORT } ` ) ;
35
+ } ) ;
36
+ }
37
+
38
+ module . exports = app ;
0 commit comments