Handle Page not found when V1 is not provided in the URL path
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';
@@ -5,7 +7,7 @@ import ExpressUtils from '../ExpressUtils';
import GlobalRouter from './v1/router/GlobalRouter';
import Logging from '../../utils/Logging';
import { ServerUtils } from '../ServerUtils';
-import express from 'express';
+import { StatusCodes } from 'http-status-codes';
import http from 'http';
import sanitize from 'express-sanitizer';
@@ -36,6 +38,13 @@ export default class CentralRestServer {
this.expressApplication.all('/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();
+ }
+ });
// 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
-import express, { NextFunction, Request, Response } from 'express';
-
import AssetRouter from './api/AssetRouter';
import AuthRouter from './auth/AuthRouter';
import AuthService from '../service/AuthService';
@@ -12,13 +10,13 @@ import Logging from '../../../../utils/Logging';
import LoggingRouter from './api/LoggingRouter';
import NotificationRouter from './api/NotificationRouter';
import OCPIEndpointRouter from './api/OCPIEndpointRouter';
-import { StatusCodes } from 'http-status-codes';
import SwaggerRouter from './doc/SwaggerRouter';
import TagRouter from './api/TagRouter';
import TenantRouter from './api/TenantRouter';
import TransactionRouter from './api/TransactionRouter';
import UserRouter from './api/UserRouter';
import UtilRouter from './util/UtilRouter';
+import express from 'express';
export default class GlobalRouter {
private router: express.Router;
@@ -32,7 +30,6 @@ export default class GlobalRouter {
this.buildRouteAPI();
this.buildRouteUtils();
this.buildRouteDocs();
- this.buildUnknownRoute();
return this.router;
}
@@ -63,19 +60,10 @@ export default class GlobalRouter {
}
protected buildRouteUtils(): void {
- this.router.use('/utils', new UtilRouter().buildRoutes());
+ this.router.use('/util', new UtilRouter().buildRoutes());
}
protected buildRouteDocs(): void {
this.router.use('/docs', new SwaggerRouter().buildRoutes());
}
-
- protected buildUnknownRoute(): void {
- this.router.use('*', (req: Request, res: Response, next: NextFunction) => {
- if (!res.headersSent) {
- res.sendStatus(StatusCodes.NOT_FOUND);
- next();
- }
- });
- }
}

Handle Page not found when V1 is not provided in the URL path