Mise à jour du main script de l'API

This commit is contained in:
2025-11-30 17:57:00 +01:00
parent f29b08556d
commit c581006121
+26 -47
View File
@@ -12,7 +12,7 @@ var express = require('express'),
errorhandler = require('errorhandler'), errorhandler = require('errorhandler'),
morgan = require('morgan'); morgan = require('morgan');
const utils = require('./src/utils'), const DateUtilities = require('./src/utils/dateUtilities'),
exceptionHandler = require('./src/middlewares/exceptions.handler'), exceptionHandler = require('./src/middlewares/exceptions.handler'),
connectDb = require('./src/database/connectDb'), connectDb = require('./src/database/connectDb'),
config = require('./src/config'); config = require('./src/config');
@@ -56,56 +56,35 @@ if (appEnv !== 'production') {
require('./src/database/models/mongo'); require('./src/database/models/mongo');
config.initAuth(); config.initAuth();
//config.initApiKey(); //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); // Exception handlers MUST be AFTER routes
app.use(exceptionHandler.logErrorHandler); app.use(exceptionHandler.notFoundErrorHandler);
//app.use(exceptionHandler.clientErrorHandler); app.use(exceptionHandler.logErrorHandler);
app.use(exceptionHandler.fianlErrorHandler); //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: {}
}});
}
});
*/
const startServer = async () => { const startServer = async () => {
const port = process.env.SERVER_PORT || 3200; try {
await promisify(app.listen).bind(app)(port); // Initialize database connections and create relationships FIRST
console.log(`${utils.getDateTimeISOString()}`, chalk.green(`${process.env.APP_ENV} API server listening on port ${port}`)); 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... // finally, let's start our server...