@@ -7,14 +7,15 @@ const path = require('path');
77
88const app = express ( ) ;
99const PORT = process . env . PORT || 3000 ;
10+ const UPLOADS_DIR = 'uploads/' ;
1011
1112// Middleware
1213app . use ( cors ( ) ) ;
1314app . use ( express . json ( ) ) ;
1415
1516// Configure multer for file upload
1617const upload = multer ( {
17- dest : 'uploads/' ,
18+ dest : UPLOADS_DIR ,
1819 limits : { fileSize : 10 * 1024 * 1024 } // 10MB limit
1920} ) ;
2021
@@ -141,12 +142,21 @@ app.post('/api/extract', upload.single('pdf'), async (req, res) => {
141142 }
142143} ) ;
143144
144- // Start server
145- app . listen ( PORT , ( ) => {
146- console . log ( `PDF Data Extractor Demo Server running on http://localhost:${ PORT } ` ) ;
147- console . log ( `Upload endpoint: http://localhost:${ PORT } /api/extract` ) ;
148- } ) ;
149-
150- // Create uploads directory if it doesn't exist
151- const uploadsDir = path . join ( __dirname , 'uploads' ) ;
152- fs . mkdir ( uploadsDir , { recursive : true } ) . catch ( console . error ) ;
145+ // Initialize server
146+ ( async ( ) => {
147+ try {
148+ // Create uploads directory before starting server
149+ const uploadsDir = path . join ( __dirname , UPLOADS_DIR ) ;
150+ await fs . mkdir ( uploadsDir , { recursive : true } ) ;
151+ console . log ( `Uploads directory ready: ${ uploadsDir } ` ) ;
152+
153+ // Start server only after uploads directory is ready
154+ app . listen ( PORT , ( ) => {
155+ console . log ( `PDF Data Extractor Demo Server running on http://localhost:${ PORT } ` ) ;
156+ console . log ( `Upload endpoint: http://localhost:${ PORT } /api/extract` ) ;
157+ } ) ;
158+ } catch ( error ) {
159+ console . error ( 'Failed to initialize server:' , error ) ;
160+ process . exit ( 1 ) ;
161+ }
162+ } ) ( ) ;
0 commit comments