Mise à jour du main script de l'API
This commit is contained in:
@@ -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();
|
|
||||||
|
|
||||||
|
// Register routes AFTER connections are established
|
||||||
|
const setupRoutes = () => {
|
||||||
app.use(require('./src/routes'));
|
app.use(require('./src/routes'));
|
||||||
|
|
||||||
|
// Exception handlers MUST be AFTER routes
|
||||||
app.use(exceptionHandler.notFoundErrorHandler);
|
app.use(exceptionHandler.notFoundErrorHandler);
|
||||||
app.use(exceptionHandler.logErrorHandler);
|
app.use(exceptionHandler.logErrorHandler);
|
||||||
//app.use(exceptionHandler.clientErrorHandler);
|
//app.use(exceptionHandler.clientErrorHandler);
|
||||||
app.use(exceptionHandler.fianlErrorHandler);
|
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 () => {
|
||||||
|
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;
|
const port = process.env.SERVER_PORT || 3200;
|
||||||
await promisify(app.listen).bind(app)(port);
|
await promisify(app.listen).bind(app)(port);
|
||||||
console.log(`${utils.getDateTimeISOString()}`, chalk.green(`${process.env.APP_ENV} API server listening on port ${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...
|
||||||
|
|||||||
Reference in New Issue
Block a user