Removed REST 404 - Issue with ExpressJs
MODIFY
src/server/rest/CentralRestServer.ts
src/server/rest/CentralRestServer.ts
-import express, { NextFunction, Request, Response } from 'express';
-
import AuthService from './v1/service/AuthService';
import CentralRestServerService from './CentralRestServerService';
import CentralSystemRestServiceConfiguration from '../../types/configuration/CentralSystemRestServiceConfiguration';
@@ -7,7 +5,7 @@ import ExpressUtils from '../ExpressUtils';
import GlobalRouter from './v1/router/GlobalRouter';
import Logging from '../../utils/Logging';
import { ServerUtils } from '../ServerUtils';
-import { StatusCodes } from 'http-status-codes';
+import express from 'express';
import http from 'http';
import sanitize from 'express-sanitizer';
@@ -31,20 +29,23 @@ export default class CentralRestServer {
// Routers
this.expressApplication.use('/v1', new GlobalRouter().buildRoutes());
// Secured API
- this.expressApplication.all('/client/api/:action',
+ this.expressApplication.use('/client/api/:action',
AuthService.authenticate(),
CentralRestServerService.restServiceSecured.bind(this));
// Util API
- this.expressApplication.all('/client/util/:action',
+ this.expressApplication.use('/client/util/:action',
Logging.traceExpressRequest.bind(this),
CentralRestServerService.restServiceUtil.bind(this));
// Unknwon Route
- this.expressApplication.use('*', (req: Request, res: Response, next: NextFunction) => {
- if (!res.headersSent) {
- res.sendStatus(StatusCodes.NOT_FOUND);
- next();
- }
- });
+ // TODO: Called before other routes: To Check
+ // eslint-disable-next-line @typescript-eslint/no-misused-promises
+ // this.expressApplication.use((req: Request, res: Response, next: NextFunction) => {
+ // if (!res.headersSent) {
+ // console.log(`Res status: ${res.statusCode}, url: ${req.url}`);
+ // res.sendStatus(StatusCodes.NOT_FOUND);
+ // }
+ // next();
+ // });
// Post init
ExpressUtils.postInitApplication(this.expressApplication);
// Create HTTP server to serve the express app

MODIFY
src/server/rest/v1/router/GlobalRouter.ts
src/server/rest/v1/router/GlobalRouter.ts
@@ -28,7 +28,7 @@ export default class GlobalRouter {
public buildRoutes(): express.Router {
this.buildRouteAuth();
this.buildRouteAPI();
- this.buildRouteUtils();
+ this.buildRouteUtil();
this.buildRouteDocs();
return this.router;
}
@@ -59,7 +59,7 @@ export default class GlobalRouter {
]);
}
- protected buildRouteUtils(): void {
+ protected buildRouteUtil(): void {
this.router.use('/util', new UtilRouter().buildRoutes());
}

MODIFY
src/server/rest/v1/router/util/UtilRouter.ts
src/server/rest/v1/router/util/UtilRouter.ts
@@ -16,8 +16,9 @@ export default class UtilRouter {
}
protected buildRoutePing(): void {
- this.router.get(`/${ServerRoute.REST_PING}`, async (req: Request, res: Response, next: NextFunction) => {
+ this.router.get(`/${ServerRoute.REST_PING}`, (req: Request, res: Response, next: NextFunction) => {
res.sendStatus(StatusCodes.OK);
+ next();
});
}
}

Removed REST 404 - Issue with ExpressJs