From c581006121b2461f54debf118deb5be65e7f7436 Mon Sep 17 00:00:00 2001 From: Julien Gautier Date: Sun, 30 Nov 2025 17:57:00 +0100 Subject: [PATCH] =?UTF-8?q?Mise=20=C3=A0=20jour=20du=20main=20script=20de?= =?UTF-8?q?=20l'API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 73 +++++++++++++++++++++------------------------------------- 1 file changed, 26 insertions(+), 47 deletions(-) diff --git a/app.js b/app.js index c074089..4ca426c 100644 --- a/app.js +++ b/app.js @@ -12,7 +12,7 @@ var express = require('express'), errorhandler = require('errorhandler'), morgan = require('morgan'); -const utils = require('./src/utils'), +const DateUtilities = require('./src/utils/dateUtilities'), exceptionHandler = require('./src/middlewares/exceptions.handler'), connectDb = require('./src/database/connectDb'), config = require('./src/config'); @@ -56,56 +56,35 @@ if (appEnv !== 'production') { require('./src/database/models/mongo'); config.initAuth(); //config.initApiKey(); -connectDb.connectAllDb(); -app.use(require('./src/routes')); +// Register routes AFTER connections are established +const setupRoutes = () => { + app.use(require('./src/routes')); -app.use(exceptionHandler.notFoundErrorHandler); -app.use(exceptionHandler.logErrorHandler); -//app.use(exceptionHandler.clientErrorHandler); -app.use(exceptionHandler.fianlErrorHandler); - -/* -var userid = new mongoose.Types.ObjectId(); -console.log('userid: ', userid._id.toString()); -*/ -/* -// catch 404 and forward to error handler -app.use(function(req, res, next) { - let err = new Error('Not Found'); - err.status = 404; - next(err); -}); - -// error handler -app.use(function(err, req, res, next) { - console.log(`${utils.getDateTimeISOString()}`, chalk.red(`error handler : ${err.message} ${err.err}`)); - if (res.headersSent) { - return next(err) - } - res.status(err.status || 500); - if (!isProduction) { - // development error handler will print stacktrace - res.json({errors: { - message: err.message, - error: err.err, - stack: err.stack - }}); - } else { - // production error handler no stacktraces leaked to user - res.json({errors: { - message: err.message, - error: {}, - stack: {} - }}); - } -}); -*/ + // Exception handlers MUST be AFTER routes + app.use(exceptionHandler.notFoundErrorHandler); + app.use(exceptionHandler.logErrorHandler); + //app.use(exceptionHandler.clientErrorHandler); + app.use(exceptionHandler.fianlErrorHandler); +}; const startServer = async () => { - const port = process.env.SERVER_PORT || 3200; - await promisify(app.listen).bind(app)(port); - console.log(`${utils.getDateTimeISOString()}`, chalk.green(`${process.env.APP_ENV} API server listening on port ${port}`)); + try { + // Initialize database connections and create relationships FIRST + await connectDb.connectAllDb(); + console.log(`${DateUtilities.getDateTimeISOString()}`, chalk.green('All database connections established, relationships loaded')); + + // NOW register the routes after connections are ready + setupRoutes(); + + // Start the HTTP server + const port = process.env.SERVER_PORT || 3200; + await promisify(app.listen).bind(app)(port); + console.log(`${DateUtilities.getDateTimeISOString()}`, chalk.green(`${process.env.APP_ENV} API server listening on port ${port}`)); + } catch (error) { + console.error(`${DateUtilities.getDateTimeISOString()}`, chalk.red('Failed to start server:'), error); + process.exit(1); + } } // finally, let's start our server...