-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
72 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,69 @@ | ||
/*** Importing necessary modules ***/ | ||
import express from 'express'; | ||
import bodyParser from 'body-parser'; | ||
import cors from 'cors'; | ||
|
||
// Import custom error handlers and logger | ||
/*** Importing custom error handlers and logger middleware ***/ | ||
import { | ||
handler404, | ||
errorsLogger, | ||
errorsHandler, | ||
} from './handlers/errors/index.js'; | ||
import { ipLogger } from './handlers/logger/ip.js'; | ||
import routes from './routes.js'; | ||
|
||
// Express APP | ||
handle404, /*** @params: req, res, next ***/ | ||
logErrors, /*** @params: err, req, res, next ***/ | ||
errorHandler, /*** @params: err, req, res, next ***/ | ||
} from './middlewares/errors.js'; | ||
import { logIP } from './middlewares/logger.js'; | ||
import routesV3 from './routes/v3/index.js'; | ||
import routesV4 from './routes/v4/index.js'; | ||
|
||
/*** Creating an instance of Express ***/ | ||
const app = express(); | ||
|
||
// Enable CORS for all routes | ||
/*** Enabling CORS for all routes ***/ | ||
app.use(cors()); | ||
|
||
// Trust the first proxy (when running behind a reverse proxy like Nginx) | ||
/*** Trusting the first proxy (when running behind a reverse proxy like Nginx) ***/ | ||
app.set('trust proxy', 1); | ||
|
||
// Middleware to parse JSON-encoded bodies | ||
/*** Middleware to parse JSON-encoded bodies ***/ | ||
app.use(express.json()); | ||
|
||
// Middleware to parse URL-encoded bodies | ||
/*** Middleware to parse URL-encoded bodies ***/ | ||
app.use(express.urlencoded({ extended: true })); | ||
|
||
// Logger middleware | ||
// Uncomment the following block if you want to enable IP logging | ||
/*** Logger middleware ***/ | ||
/** | ||
* @middleware | ||
* @description Enables IP logging for incoming requests. | ||
* @function | ||
* @param {Object} req - Express request object. | ||
* @param {Object} res - Express response object. | ||
* @param {Function} next - Express next middleware function. | ||
*/ | ||
if (process.env.LOGGER === 'true') { | ||
app.use(ipLogger); | ||
app.use(logIP); | ||
} | ||
|
||
// Custom API routes | ||
app.use(routes); | ||
/*** Middleware to parse the request body ***/ | ||
app.use(bodyParser.json()); | ||
|
||
/*** Custom API routes ***/ | ||
app.use('/api/v4', routesV4); | ||
app.use(routesV3); | ||
|
||
// Error handling middleware | ||
app.use(handler404, errorsLogger, errorsHandler); | ||
/** | ||
* @apiMiddleware | ||
* @function | ||
* @param {Object} req - Express request object. | ||
* @param {Object} res - Express response object. | ||
* @param {Function} next - Express next middleware function. | ||
* | ||
* @apiSuccess {Object[]} response - Response data. | ||
* | ||
* @apiError {BadRequest} BadRequest - Invalid request parameters. | ||
* @apiError {Unauthorized} Unauthorized - Only authenticated users can access the data. | ||
* @apiError {Forbidden} Forbidden - Only authorized users can access the data. | ||
* @apiError {NotFound} NotFound - The requested resource was not found. | ||
* @apiError {InternalServerError} InternalServerError - Something went wrong on the server. | ||
*/ | ||
app.use(handle404, logErrors, errorHandler); | ||
|
||
export default app; | ||
/*** Exporting the Express app instance ***/ | ||
export default app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters