From e250908de38300c0a3e588c1c2521b8195a4a034 Mon Sep 17 00:00:00 2001 From: Martin Donnelly Date: Thu, 18 Mar 2021 10:13:52 +0000 Subject: [PATCH] Making some progress --- .eslintignore | 0 .eslintrc | 63 ++-------- .eslintrc-orig | 55 ++++++++ .gitignore | 2 + package.json | 20 ++- server/db/connect.d.ts | 3 +- server/db/connect.js | 7 +- server/db/connect.js.map | 2 +- server/db/loginmanager.d.ts | 3 +- server/db/loginmanager.js.map | 2 +- server/handlers/LoginHandler.d.ts | 2 +- server/handlers/LoginHandler.js.map | 2 +- server/server.d.ts | 4 +- server/server.js | 17 ++- server/server.js.map | 2 +- src/controllers/AccessController.ts | 12 ++ src/controllers/ECGController.ts | 9 ++ src/controllers/IncidentsController.ts | 15 +++ src/controllers/IncidentsV2Controller.ts | 153 +++++++++++++++++++++++ src/controllers/ReceiveController.ts | 34 +++++ src/controllers/TestController.ts | 9 ++ src/controllers/TestsController.ts | 34 +++++ src/controllers/UsersController.ts | 34 +++++ src/controllers/ValuesController.ts | 34 +++++ src/db/connect.ts | 3 +- src/db/loginmanager.ts | 18 +-- src/handlers/LoginHandler.ts | 5 +- src/lib/constants.ts | 10 ++ src/middleware/Checks.ts | 26 ++++ src/middleware/jwt.ts | 60 +++++++++ src/models/Casualty.ts | 22 ++++ src/models/Chat.ts | 10 ++ src/models/ChatStatus.ts | 10 ++ src/models/IncidentItem.ts | 9 ++ src/models/IncidentItemV2.ts | 23 ++++ src/models/PanelLatest.ts | 22 ++++ src/models/PanelLayout.ts | 32 +++++ src/models/User.ts | 7 ++ src/routes/index.ts | 62 +++++++++ src/server.ts | 34 +++-- 40 files changed, 774 insertions(+), 97 deletions(-) create mode 100644 .eslintignore mode change 100755 => 100644 .eslintrc create mode 100755 .eslintrc-orig create mode 100644 src/controllers/AccessController.ts create mode 100644 src/controllers/ECGController.ts create mode 100644 src/controllers/IncidentsController.ts create mode 100644 src/controllers/IncidentsV2Controller.ts create mode 100644 src/controllers/ReceiveController.ts create mode 100644 src/controllers/TestController.ts create mode 100644 src/controllers/TestsController.ts create mode 100644 src/controllers/UsersController.ts create mode 100644 src/controllers/ValuesController.ts create mode 100644 src/lib/constants.ts create mode 100644 src/middleware/Checks.ts create mode 100644 src/middleware/jwt.ts create mode 100644 src/models/Casualty.ts create mode 100644 src/models/Chat.ts create mode 100644 src/models/ChatStatus.ts create mode 100644 src/models/IncidentItem.ts create mode 100644 src/models/IncidentItemV2.ts create mode 100644 src/models/PanelLatest.ts create mode 100644 src/models/PanelLayout.ts create mode 100644 src/models/User.ts create mode 100644 src/routes/index.ts diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..e69de29 diff --git a/.eslintrc b/.eslintrc old mode 100755 new mode 100644 index 795f96d..11643ef --- a/.eslintrc +++ b/.eslintrc @@ -1,55 +1,12 @@ { - "parserOptions": { - "ecmaVersion": 2018, - "sourceType": "module", - "ecmaFeatures": { - "jsx": false - } - }, - "env": { - "browser": true, - "node": true, - "es6": true - }, - "rules": { - "arrow-spacing": "error", - "block-scoped-var": "error", - "block-spacing": "error", - "brace-style": ["error", "stroustrup", {}], - "camelcase": "error", - "comma-dangle": ["error", "never"], - "comma-spacing": ["error", { "before": false, "after": true }], - "comma-style": [1, "last"], - "consistent-this": [1, "_this"], - "curly": [1, "multi"], - "eol-last": 1, - "eqeqeq": 1, - "func-names": 1, - "indent": ["error", 2, { "SwitchCase": 1 }], - "lines-around-comment": ["error", { "beforeBlockComment": true, "allowArrayStart": true }], - "max-len": [1, 180, 2, { "ignoreTemplateLiterals": true, "ignoreStrings": true, "ignoreUrls": true, "ignoreTrailingComments": true, "ignoreComments": true }], // 2 spaces per tab, max 80 chars per line - "new-cap": 1, - "newline-before-return": "error", - "no-array-constructor": 1, - "no-inner-declarations": [1, "both"], - "no-mixed-spaces-and-tabs": 1, - "no-multi-spaces": 2, - "no-new-object": 1, - "no-shadow-restricted-names": 1, - "object-curly-spacing": ["error", "always"], - "padded-blocks": ["error", { "blocks": "never", "switches": "always" }], - "prefer-const": "error", - "prefer-template": "error", - "one-var": 0, - "quote-props": ["error", "always"], - "quotes": [1, "single"], - "radix": 1, - "semi": [1, "always"], - "space-before-blocks": [1, "always"], - "space-infix-ops": 1, - "vars-on-top": 1, - "no-multiple-empty-lines": ["error", { "max": 1, "maxEOF": 1 }], - "spaced-comment": ["error", "always", { "markers": ["/"] }] - } - + "root": true, + "parser": "@typescript-eslint/parser", + "plugins": [ + "@typescript-eslint" + ], + "extends": [ + "eslint:recommended", + "plugin:@typescript-eslint/eslint-recommended", + "plugin:@typescript-eslint/recommended" + ] } diff --git a/.eslintrc-orig b/.eslintrc-orig new file mode 100755 index 0000000..795f96d --- /dev/null +++ b/.eslintrc-orig @@ -0,0 +1,55 @@ +{ + "parserOptions": { + "ecmaVersion": 2018, + "sourceType": "module", + "ecmaFeatures": { + "jsx": false + } + }, + "env": { + "browser": true, + "node": true, + "es6": true + }, + "rules": { + "arrow-spacing": "error", + "block-scoped-var": "error", + "block-spacing": "error", + "brace-style": ["error", "stroustrup", {}], + "camelcase": "error", + "comma-dangle": ["error", "never"], + "comma-spacing": ["error", { "before": false, "after": true }], + "comma-style": [1, "last"], + "consistent-this": [1, "_this"], + "curly": [1, "multi"], + "eol-last": 1, + "eqeqeq": 1, + "func-names": 1, + "indent": ["error", 2, { "SwitchCase": 1 }], + "lines-around-comment": ["error", { "beforeBlockComment": true, "allowArrayStart": true }], + "max-len": [1, 180, 2, { "ignoreTemplateLiterals": true, "ignoreStrings": true, "ignoreUrls": true, "ignoreTrailingComments": true, "ignoreComments": true }], // 2 spaces per tab, max 80 chars per line + "new-cap": 1, + "newline-before-return": "error", + "no-array-constructor": 1, + "no-inner-declarations": [1, "both"], + "no-mixed-spaces-and-tabs": 1, + "no-multi-spaces": 2, + "no-new-object": 1, + "no-shadow-restricted-names": 1, + "object-curly-spacing": ["error", "always"], + "padded-blocks": ["error", { "blocks": "never", "switches": "always" }], + "prefer-const": "error", + "prefer-template": "error", + "one-var": 0, + "quote-props": ["error", "always"], + "quotes": [1, "single"], + "radix": 1, + "semi": [1, "always"], + "space-before-blocks": [1, "always"], + "space-infix-ops": 1, + "vars-on-top": 1, + "no-multiple-empty-lines": ["error", { "max": 1, "maxEOF": 1 }], + "spaced-comment": ["error", "always", { "markers": ["/"] }] + } + +} diff --git a/.gitignore b/.gitignore index 8869f59..df8d3bb 100755 --- a/.gitignore +++ b/.gitignore @@ -164,3 +164,5 @@ artefacts/*.json !/artefacts/ menu.db menu.db.backup + +/server diff --git a/package.json b/package.json index 5285bdc..0786bf0 100644 --- a/package.json +++ b/package.json @@ -3,19 +3,30 @@ "version": "1.0.0", "description": "", "main": "./server/index.js", + "watch": { + "run_server": { + "patterns": [ + "src" + ], + "extensions": "ts,html,scss", + "quiet": false + } + }, "scripts": { "run:ts": "ts-node ./src/development.ts", "clean": "rm -rf dist", "prebuild": "rimraf ./server", "test:js": "mocha test/**/*.js", "test:ts": "mocha -r ts-node/register test/**/*.ts", - "lint": "eslint . --ext .ts", + "lint": "eslint ./src --ext .ts", "coverage": "nyc npm run test:js", "build:js": "rollup -c rollup.config.js", "compile:es": "tsc --declaration true --declarationMap true --module esnext --outDir './server/es'", "compile:commonjs": "tsc --declaration true --declarationMap true --module commonjs --outDir './server/commonjs'", "compile:ts": "tsc --outDir './server'", - "compile": "npm run prebuild && npm run compile:es && npm run compile:commonjs" + "compile": "npm run prebuild && npm run compile:es && npm run compile:commonjs", + "run_server": "npm run run:ts", + "watch_server": "npm-watch run_server" }, "keywords": [], "author": "", @@ -44,10 +55,13 @@ "devDependencies": { "@types/cors": "^2.8.10", "@types/express": "^4.17.8", + "@types/express-serve-static-core": "^4.17.18", "@types/express-session": "^1.17.0", "@types/jsonwebtoken": "^8.5.0", "@types/morgan": "^1.9.2", - "eslint": "^7.21.0", + "@typescript-eslint/eslint-plugin": "^4.18.0", + "@typescript-eslint/parser": "^4.18.0", + "eslint": "7.22.0", "prettier": "^2.2.1", "rimraf": "^3.0.2", "rollup": "^2.33.1", diff --git a/server/db/connect.d.ts b/server/db/connect.d.ts index f90be64..f0bdfe0 100644 --- a/server/db/connect.d.ts +++ b/server/db/connect.d.ts @@ -1 +1,2 @@ -export declare const db: any; +import sqlite3 from 'sqlite3'; +export declare const db: sqlite3.Database; diff --git a/server/db/connect.js b/server/db/connect.js index 6a0946f..510f89a 100644 --- a/server/db/connect.js +++ b/server/db/connect.js @@ -1,7 +1,10 @@ "use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; Object.defineProperty(exports, "__esModule", { value: true }); exports.db = void 0; -const sqlite3 = require('sqlite3').verbose(); +const sqlite3_1 = __importDefault(require("sqlite3")); console.log(`${__dirname}/../../db/users.db`); -exports.db = new sqlite3.Database(`${__dirname}/../../db/users.db`); +exports.db = new sqlite3_1.default.Database(`${__dirname}/../../db/users.db`); //# sourceMappingURL=connect.js.map \ No newline at end of file diff --git a/server/db/connect.js.map b/server/db/connect.js.map index 1a89e25..539ef5b 100644 --- a/server/db/connect.js.map +++ b/server/db/connect.js.map @@ -1 +1 @@ -{"version":3,"file":"connect.js","sourceRoot":"","sources":["../../src/db/connect.ts"],"names":[],"mappings":";;;AAAA,MAAM,OAAO,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC,OAAO,EAAE,CAAC;AAC7C,OAAO,CAAC,GAAG,CAAC,GAAG,SAAS,oBAAoB,CAAC,CAAC;AACjC,QAAA,EAAE,GAAG,IAAI,OAAO,CAAC,QAAQ,CAAC,GAAG,SAAS,oBAAoB,CAAC,CAAC"} \ No newline at end of file +{"version":3,"file":"connect.js","sourceRoot":"","sources":["../../src/db/connect.ts"],"names":[],"mappings":";;;;;;AACA,sDAA8B;AAC9B,OAAO,CAAC,GAAG,CAAC,GAAG,SAAS,oBAAoB,CAAC,CAAC;AACjC,QAAA,EAAE,GAAG,IAAI,iBAAO,CAAC,QAAQ,CAAC,GAAG,SAAS,oBAAoB,CAAC,CAAC"} \ No newline at end of file diff --git a/server/db/loginmanager.d.ts b/server/db/loginmanager.d.ts index c3e741a..43b0c41 100644 --- a/server/db/loginmanager.d.ts +++ b/server/db/loginmanager.d.ts @@ -1 +1,2 @@ -export declare const getOne: (username: string, password: string) => Promise; +import { User } from '../models/User'; +export declare const getOne: (username: string, password: string) => User | any; diff --git a/server/db/loginmanager.js.map b/server/db/loginmanager.js.map index e9c0ce6..a91277b 100644 --- a/server/db/loginmanager.js.map +++ b/server/db/loginmanager.js.map @@ -1 +1 @@ -{"version":3,"file":"loginmanager.js","sourceRoot":"","sources":["../../src/db/loginmanager.ts"],"names":[],"mappings":";;;AAAA,uCAA6B;AAEtB,MAAM,MAAM,GAAG,CAAC,QAAe,EAAE,QAAe,EAAE,EAAE;IACvD,MAAM,GAAG,GAAG,6DAA6D,CAAC;IAE1E,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACnC,YAAE,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,QAAQ,EAAE,QAAQ,CAAC,EAAE,CAAC,GAAQ,EAAE,GAAY,EAAE,EAAE;YACzD,IAAI,GAAG;gBACH,MAAM,CAAC,GAAG,CAAC,CAAC;YAEhB,IAAI,CAAC,GAAG;gBAAE,OAAO,CAAC,GAAG,CAAC,CAAC;QAC3B,CAAC,CAAC,CAAC;IACP,CAAC,CAAC,CAAC;AACP,CAAC,CAAC;AAXW,QAAA,MAAM,UAWjB"} \ No newline at end of file +{"version":3,"file":"loginmanager.js","sourceRoot":"","sources":["../../src/db/loginmanager.ts"],"names":[],"mappings":";;;AAAA,uCAA+B;AAGxB,MAAM,MAAM,GAAG,CAAC,QAAgB,EAAE,QAAgB,EAAc,EAAE;IACvE,MAAM,GAAG,GAAG,6DAA6D,CAAC;IAE1E,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACrC,YAAE,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,QAAQ,EAAE,QAAQ,CAAC,EAAE,CAAC,GAAQ,EAAE,GAAS,EAAE,EAAE;YACxD,IAAI,GAAG;gBAAE,MAAM,CAAC,GAAG,CAAC,CAAC;YAErB,IAAI,CAAC,GAAG;gBAAE,OAAO,CAAC,GAAG,CAAC,CAAC;QACzB,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC;AAVW,QAAA,MAAM,UAUjB"} \ No newline at end of file diff --git a/server/handlers/LoginHandler.d.ts b/server/handlers/LoginHandler.d.ts index 85946dc..da671f1 100644 --- a/server/handlers/LoginHandler.d.ts +++ b/server/handlers/LoginHandler.d.ts @@ -1,3 +1,3 @@ import express from 'express'; -export declare const ProcessLogin: (req: express.Request, res: express.Response) => express.Response> | Promise> | undefined>; +export declare const ProcessLogin: (req: express.Request, res: express.Response) => any; export declare const authenticate: (req: express.Request, res: express.Response, next: express.NextFunction) => void; diff --git a/server/handlers/LoginHandler.js.map b/server/handlers/LoginHandler.js.map index 6557006..9b0966f 100644 --- a/server/handlers/LoginHandler.js.map +++ b/server/handlers/LoginHandler.js.map @@ -1 +1 @@ -{"version":3,"file":"LoginHandler.js","sourceRoot":"","sources":["../../src/handlers/LoginHandler.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAEA,iEAAmD;AAEnD,wDAA2D;AAEpD,MAAM,YAAY,GAAG,CAAC,GAAoB,EAAE,GAAqB,EAAE,EAAE;IAE1E,OAAO,CAAC,GAAG,CAAC,cAAc,EAAE,GAAG,CAAC,IAAI,CAAC,CAAC;IAEtC,MAAM,QAAQ,GAAW,GAAG,CAAC,IAAI,CAAC,QAAQ,CAAC;IAC3C,MAAM,QAAQ,GAAW,GAAG,CAAC,IAAI,CAAC,QAAQ,CAAC;IAE3C,OAAO,CAAC,GAAG,CAAC,UAAU,EAAE,QAAQ,CAAC,CAAC;IAClC,OAAO,CAAC,GAAG,CAAC,UAAU,EAAE,QAAQ,CAAC,CAAC;IAClC,IAAI,QAAQ,IAAI,QAAQ,EAAE;Q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AElC,MAAM,IAAI,GAAQ,MAAM,YAAY,CAAC,MAAM,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;IAEhE,IAAI,CAAC,IAAI;QAAE,MAAM,mCAAmC,CAAC;IAErD,MAAM,EAAE,GAAG,IAAI,CAAC,EAAG,CAAC;IAGpB,MAAM,KAAK,GAAG,mCAAmB,CAAC,QAAQ,CAAC,CAAC;IAE5C,OAAO,CAAC,GAAG,CAAC,2CAA2C,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;IAE5E,OAAO,EAAE,EAAE,EAAE,EAAE,EAAE,KAAK,EAAE,CAAC;AAC3B,CAAC;AAEM,MAAM,YAAY,GAAG,CAAC,GAAoB,EAAE,GAAqB,EAAE,IAA0B,EAAE,EAAE;IACtG,OAAO,CAAC,GAAG,CAAC,+BAA+B,CAAC,CAAC;IAC7C,MAAM,EAAE,QAAQ,EAAE,QAAQ,EAAE,GAAG,GAAG,CAAC,IAAI,CAAC;IACxC,cAAc,CAAC,QAAQ,EAAE,QAAQ,CAAC;SAC/B,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;SAC9B,KAAK,CAAC,IAAI,CAAC,CAAC;AACjB,CAAC,CAAC;AANW,QAAA,YAAY,gBAMvB"} \ No newline at end of file diff --git a/server/server.d.ts b/server/server.d.ts index 2df6ed7..2f98b85 100644 --- a/server/server.d.ts +++ b/server/server.d.ts @@ -1,5 +1,5 @@ -import express from 'express'; +import { Application } from 'express'; export declare class App { - protected app: express.Application; + protected app: Application; constructor(NODE_ENV?: string, PORT?: number); } diff --git a/server/server.js b/server/server.js index 70808aa..f058f3a 100644 --- a/server/server.js +++ b/server/server.js @@ -28,28 +28,35 @@ const express_1 = __importDefault(require("express")); const helmet_1 = __importDefault(require("helmet")); const cors_1 = __importDefault(require("cors")); const morgan_1 = __importDefault(require("morgan")); -const authenticator_1 = require("./lib/authenticator"); const path = __importStar(require("path")); const login_1 = require("./routes/login"); +const index_1 = __importDefault(require("./routes/index")); +const constants_1 = require("./lib/constants"); dotenv.config(); class App { constructor(NODE_ENV = 'development', PORT = 8080) { const serverPort = process.env.PORT || PORT; const sitePath = 'public'; + const corsOptions = { credentials: false }; this.app = express_1.default(); this.app.use(morgan_1.default('dev')); this.app.use(express_1.default.json()); - this.app.use(cors_1.default()); + this.app.use(express_1.default.urlencoded({ extended: false })); + this.app.use(cors_1.default(corsOptions)); this.app.use(helmet_1.default()); this.app.set('trust proxy', 1); - this.app.post('/test', authenticator_1.authenticateToken, (req, res) => { - res.sendStatus(200); - }); const login = login_1.Login(this.app); this.app.get('/', (req, res) => { console.log('p', path.join(process.cwd(), sitePath)); res.sendFile('index.html', { root: path.join(process.cwd(), sitePath) }); }); + this.app.use(`${process.env.URL_PREFIX}`, index_1.default); + this.app.use('*', (req, res) => { + return res.status(404).json({ + success: false, + message: constants_1.ERROR_MESSAGES.ENDPOINT_NOT_FOUND, + }); + }); this.app.listen(serverPort, function () { console.log('The server is running in port localhost: ', serverPort); }); diff --git a/server/server.js.map b/server/server.js.map index e7053d6..a426528 100644 --- a/server/server.js.map +++ b/server/server.js.map @@ -1 +1 @@ -{"version":3,"file":"server.js","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA,+CAAiC;AAEjC,sDAA8B;AAC9B,oDAA4B;AAC5B,gDAAwB;AACxB,oDAA4B;AAI5B,uDAAwD;AACxD,2CAA6B;AAE7B,0CAAqC;AAErC,MAAM,CAAC,MAAM,EAAE,CAAC;AAEhB,MAAa,GAAG;IAGd,YAAY,WAAmB,aAAa,EAAE,OAAe,IAAI;QAC/D,MAAM,UAAU,GAAG,OAAO,CAAC,GAAG,CAAC,IAAI,IAAI,IAAI,CAAC;QAC5C,MAAM,QAAQ,GAAG,QAAQ,CAAC;QAE1B,IAAI,CAAC,GAAG,GAAG,iBAAO,EAAE,CAAC;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,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,QAAQ,CAAC,CAAC,CAAC;YACrD,GAAG,CAAC,QAAQ,CAAC,YAAY,EAAE,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,QAAQ,CAAC,EAAE,CAAC,CAAC;QAC3E,CAAC,CAAC,CAAC;QAEH,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,OAAO,CAAC,GAAG,CAAC,UAAU,EAAE,EAAE,eAAW,CAAC,CAAC;QAEvD,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE;YAC7B,OAAO,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC;gBAC1B,OAAO,EAAE,KAAK;gBACd,OAAO,EAAE,0BAAc,CAAC,kBAAkB;aAC3C,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;QAEH,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,UAAU,EAAE;YAC1B,OAAO,CAAC,GAAG,CAAC,2CAA2C,EAAE,UAAU,CAAC,CAAC;QACvE,CAAC,CAAC,CAAC;IACL,CAAC;CA2BF;AAlED,kBAkEC"} \ No newline at end of file diff --git a/src/controllers/AccessController.ts b/src/controllers/AccessController.ts new file mode 100644 index 0000000..55a34dc --- /dev/null +++ b/src/controllers/AccessController.ts @@ -0,0 +1,12 @@ +import express from "express"; + +export default { + onLogin: async (req: any, res: express.Response): Promise => { + return res.status(200).json({ + success: true, + token: req.authToken, + userId: req.userId, + }) + } +} + diff --git a/src/controllers/ECGController.ts b/src/controllers/ECGController.ts new file mode 100644 index 0000000..ab6124d --- /dev/null +++ b/src/controllers/ECGController.ts @@ -0,0 +1,9 @@ +const MyID = 'ECGController'; +export default { + onGetECG: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onGetECG` + }) + } +} diff --git a/src/controllers/IncidentsController.ts b/src/controllers/IncidentsController.ts new file mode 100644 index 0000000..333a47b --- /dev/null +++ b/src/controllers/IncidentsController.ts @@ -0,0 +1,15 @@ +const MyID = 'IncidentsController'; +export default { + onGetIncident: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onGetIncident` + }) + }, + onUploadFiles: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onUploadFiles` + }) + } +} diff --git a/src/controllers/IncidentsV2Controller.ts b/src/controllers/IncidentsV2Controller.ts new file mode 100644 index 0000000..adeb6a2 --- /dev/null +++ b/src/controllers/IncidentsV2Controller.ts @@ -0,0 +1,153 @@ +const MyID = 'IncidentsV2Controller'; +export default { + onUpdateCasualty: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onUpdateCasualty` + }) + }, + onStartIncident: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onStartIncident` + }) + }, + onSubmit: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onSubmit` + }) + }, + onControllerVersion: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onControllerVersion` + }) + }, + onGetChats: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onGetChats` + }) + }, + onGetCasualty: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onGetCasualty` + }) + }, + onGetGroundChat: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onGetGroundChat` + }) + }, + onChat: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onChat` + }) + }, + onGetPanelInfo: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onGetPanelInfo` + }) + }, + onListIncidents: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onListIncidents` + }) + }, + onListClosedIncidents: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onListClosedIncidents` + }) + }, + onListActiveIncidents: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onListActiveIncidents` + }) + }, + onListEvents: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onListEvents` + }) + }, + onListClosedEvents: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onStartIncident` + }) + }, + onAcknowledgeAir: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onAcknowledgeAir` + }) + }, + onAcknowledgeGround: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onAcknowledgeGround` + }) + }, + onTypingAir: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onTypingAir` + }) + }, + onTypingGround: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onTypingGround` + }) + }, + onNotTypingGround: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onNotTypingGround` + }) + }, + onCheckTypingGround: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onCheckTypingGround` + }) + }, + onCheckTypingAir: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onCheckTypingAir` + }) + }, + onOnlineAir: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onOnlineAir` + }) + }, + onOnlineGround: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onOnlineGround` + }) + }, + onCheckOnlineGround: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onCheckOnlineGround` + }) + }, + onCheckOnlineAir: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onCheckOnlineAir` + }) + } +} diff --git a/src/controllers/ReceiveController.ts b/src/controllers/ReceiveController.ts new file mode 100644 index 0000000..d5ce5f0 --- /dev/null +++ b/src/controllers/ReceiveController.ts @@ -0,0 +1,34 @@ +const MyID = 'ReceiveController'; +export default { + onGet: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onGet` + }) + }, + onGetById: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onGetById` + }) + }, + onPost: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onPost` + }) + }, + onPut: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onPut` + }) + }, + onDelete: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onDelete` + }) + } + +} diff --git a/src/controllers/TestController.ts b/src/controllers/TestController.ts new file mode 100644 index 0000000..ff6930c --- /dev/null +++ b/src/controllers/TestController.ts @@ -0,0 +1,9 @@ +const MyID = 'TestController'; +export default { + onGet: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onGet` + }) + } +} diff --git a/src/controllers/TestsController.ts b/src/controllers/TestsController.ts new file mode 100644 index 0000000..0109eec --- /dev/null +++ b/src/controllers/TestsController.ts @@ -0,0 +1,34 @@ +const MyID = 'TestsController'; +export default { + onGet: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onGet` + }) + }, + onGetById: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onGetById` + }) + }, + onPost: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onPost` + }) + }, + onPut: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onPut` + }) + }, + onDelete: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onDelete` + }) + } + +} diff --git a/src/controllers/UsersController.ts b/src/controllers/UsersController.ts new file mode 100644 index 0000000..bd0fefb --- /dev/null +++ b/src/controllers/UsersController.ts @@ -0,0 +1,34 @@ +const MyID = 'UsersController'; +export default { + onGet: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onGet` + }) + }, + onGetById: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onGetById` + }) + }, + onPost: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onPost` + }) + }, + onPut: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onPut` + }) + }, + onDelete: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onDelete` + }) + } + +} diff --git a/src/controllers/ValuesController.ts b/src/controllers/ValuesController.ts new file mode 100644 index 0000000..0f3a378 --- /dev/null +++ b/src/controllers/ValuesController.ts @@ -0,0 +1,34 @@ +const MyID = 'ValuesController'; +export default { + onGet: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onGet` + }) + }, + onGetById: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onGetById` + }) + }, + onPost: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onPost` + }) + }, + onPut: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onPut` + }) + }, + onDelete: async (req: any, res: any, next: any) => { + return res.status(200).json({ + success: true, + message: `${MyID}::onDelete` + }) + } + +} diff --git a/src/db/connect.ts b/src/db/connect.ts index 369a932..0be581c 100644 --- a/src/db/connect.ts +++ b/src/db/connect.ts @@ -1,3 +1,4 @@ -const sqlite3 = require('sqlite3').verbose(); +// const sqlite3 = require('sqlite3').verbose(); +import sqlite3 from 'sqlite3'; console.log(`${__dirname}/../../db/users.db`); export const db = new sqlite3.Database(`${__dirname}/../../db/users.db`); diff --git a/src/db/loginmanager.ts b/src/db/loginmanager.ts index 670e09e..caa105e 100644 --- a/src/db/loginmanager.ts +++ b/src/db/loginmanager.ts @@ -1,14 +1,14 @@ -import {db} from './connect'; +import { db } from './connect'; +import { User } from '../models/User'; -export const getOne = (username:string, password:string) => { - const sql = 'SELECT * FROM accounts WHERE username = ? and password = ?'; +export const getOne = (username: string, password: string): User | any => { + const sql = 'SELECT * FROM accounts WHERE username = ? and password = ?'; - return new Promise((resolve, reject) => { - db.get(sql, [username, password], (err: any, row: unknown) => { - if (err) - reject(err); + return new Promise((resolve, reject) => { + db.get(sql, [username, password], (err: any, row: User) => { + if (err) reject(err); - if (!err) resolve(row); - }); + if (!err) resolve(row); }); + }); }; diff --git a/src/handlers/LoginHandler.ts b/src/handlers/LoginHandler.ts index 9cf6fec..47e1475 100644 --- a/src/handlers/LoginHandler.ts +++ b/src/handlers/LoginHandler.ts @@ -1,6 +1,7 @@ import express from 'express'; import * as loginManager from '../db/loginmanager'; +import {User} from '../models/User'; import { generateAccessToken } from '../lib/authenticator'; @@ -17,7 +18,7 @@ export const ProcessLogin = (req: express.Request, res: express.Response) => { console.log('>> can try to login'); return loginManager .getOne(username, password) - .then((data) => { + .then((data:User) => { if (!data) { // response.redirect('/'); console.warn('No data!'); @@ -31,7 +32,7 @@ export const ProcessLogin = (req: express.Request, res: express.Response) => { response.redirect('/menu');*/ } }) - .catch((err) => { + .catch((err:any) => { console.error(err); return res.status(500).send({ message: err.message || 'Some error occurred while querying the database.', diff --git a/src/lib/constants.ts b/src/lib/constants.ts new file mode 100644 index 0000000..68f820d --- /dev/null +++ b/src/lib/constants.ts @@ -0,0 +1,10 @@ +export const ERROR_MESSAGES = { + USER_NOT_FOUND: 'User does not exist', + NO_TOKEN: 'No access token provided', + UNAUTHORIZED: "Sorry, we can't find an account with these credentials. Please try again or create a new account.", + ENDPOINT_NOT_FOUND: "API endpoint doesn't exist", + ERRA1: "Incident or item does not exist", + ERRA2: "Invalid timestamp", + ERRA3: "Item already acknowledged", + ERRTG1: "No incident supplied" +}; diff --git a/src/middleware/Checks.ts b/src/middleware/Checks.ts new file mode 100644 index 0000000..fa956c5 --- /dev/null +++ b/src/middleware/Checks.ts @@ -0,0 +1,26 @@ +import { ERROR_MESSAGES } from '../lib/constants'; + +const IAM = 'Checks'; + +export default { + incidentExists: async (req: any, res: any, next: any) => { + console.log(`${IAM}::incidentExists`); + + next(); + }, + alreadyAcknowledged: async (req: any, res: any, next: any) => { + console.log(`${IAM}::alreadyAcknowledged`); + + next(); + }, + validTimestamp: async (req: any, res: any, next: any) => { + console.log(`${IAM}::validTimestamp`); + + next(); + }, + incidentNotNull: async (req: any, res: any, next: any) => { + console.log(`${IAM}::incidentNotNull`); + + next(); + }, +}; diff --git a/src/middleware/jwt.ts b/src/middleware/jwt.ts new file mode 100644 index 0000000..1edb135 --- /dev/null +++ b/src/middleware/jwt.ts @@ -0,0 +1,60 @@ +import jwt from 'jsonwebtoken'; +import { ERROR_MESSAGES } from '../lib/constants'; + +import { getOne } from '../db/loginmanager'; +import {User} from '../models/User'; +import express from "express"; + +const IAM = 'jwt'; + +export const encode = async (req: any, res: express.Response, next: express.NextFunction) => { + try { + console.log(`>> ${IAM}::encode`); + const username: string = req.body.username; + const password: string = req.body.password; + + console.log(`>> ${IAM}::encode - username`, username); + console.log(`>> ${IAM}::encode - password`, password); + + const user:User = await getOne(username,password); + + if (user) { + console.log(`>> ${IAM}::encode - user`, user); + + const authToken = jwt.sign( + { + userId: user._id + }, + process.env.TOKEN_SECRET! + ); + console.log('Auth', authToken); + + req.authToken = authToken; + req.userId = user._id; + next(); + + } else { + throw new Error(ERROR_MESSAGES.UNAUTHORIZED); + } + } catch (error) { + console.log(error); + return res.status(400).json({ success: false, message: error.message }); + } +}; + + +export const decode = (req: any, res: any, next: any) => { + if (!req.headers['authorization']) { + return res.status(401).json({ success: false, message: ERROR_MESSAGES.NO_TOKEN }); + } + try { + const accessToken = req.headers.authorization.split(' ')[1]; + const decoded: any = jwt.verify(accessToken, process.env.TOKEN_SECRET!); + req.userId = decoded.userId; + return next(); + } catch (error) { + console.log(error); + return res.status(401).json({ success: false, message: error.message }); + } + +}; diff --git a/src/models/Casualty.ts b/src/models/Casualty.ts new file mode 100644 index 0000000..eef6134 --- /dev/null +++ b/src/models/Casualty.ts @@ -0,0 +1,22 @@ +export interface Casualty { + id: number; + incidentid: string; + gender: string; + dob: string; + age: number; + alcohol: string; + allergies: string; + allergydetails: string; + medicinetaken: string; + medhist: string; + flight: string; + seat: string; + arrivalairport: string; + lastmeal: string; + detailsadded: number; + detailssent: number; + detailsack: number; + deleted: number; + complain: string; + history: string; +} diff --git a/src/models/Chat.ts b/src/models/Chat.ts new file mode 100644 index 0000000..eeef6b7 --- /dev/null +++ b/src/models/Chat.ts @@ -0,0 +1,10 @@ +export interface Chat { + id: number; + incidentid: string; + aibermsg:string; + controlmsg: string; + msgtime:number; + msgrecd:number; + msgread:number; + deleted:number; +} diff --git a/src/models/ChatStatus.ts b/src/models/ChatStatus.ts new file mode 100644 index 0000000..f1bb4a2 --- /dev/null +++ b/src/models/ChatStatus.ts @@ -0,0 +1,10 @@ +export interface ChatStatus { + id: number; + incidentid: string; + typingair: number; + typingground: number; + lasttypingair: number; + lasttypingground: number; + deleted: number; + +} diff --git a/src/models/IncidentItem.ts b/src/models/IncidentItem.ts new file mode 100644 index 0000000..d95b963 --- /dev/null +++ b/src/models/IncidentItem.ts @@ -0,0 +1,9 @@ +export interface IncidentItem { + id: number; + incidentdate: Date; + description: string; + username: string; + reportdate: Date; + reportlog: string; + ecgtrace: string; +} diff --git a/src/models/IncidentItemV2.ts b/src/models/IncidentItemV2.ts new file mode 100644 index 0000000..00d0fed --- /dev/null +++ b/src/models/IncidentItemV2.ts @@ -0,0 +1,23 @@ +export interface IncidentItem { + Id: number; + incidentid: string; + incidenttime: number; + username: string; + reporttime: number; + latitude: number; + longitude: number; + message_type: string; + entry_type: string; + entry_val1: string; + entry_val2: string; + ackground: number; + ackair: number; + battery: number; + pluggedin: boolean; + reportlog: string; + ecgtrace: string; + typingair: number; + typingground: number; + onlineair: number; + onlineground: number; +} diff --git a/src/models/PanelLatest.ts b/src/models/PanelLatest.ts new file mode 100644 index 0000000..862e876 --- /dev/null +++ b/src/models/PanelLatest.ts @@ -0,0 +1,22 @@ +export interface PanelLatest { + panelinfoid: number; + incidentid: string; + avpu: string; + avputime: number; + ecg: number; + ecgtime: number; + hr: number; + hrtime: number; + br: number; + brtime: number; + sats: number; + satstime: number; + bpsys: number; + bpdia: number; + bptime: number; + temp: number; + temptime: number; + messages: number; + messagestime: number; + deleted: number; +} diff --git a/src/models/PanelLayout.ts b/src/models/PanelLayout.ts new file mode 100644 index 0000000..6f76fb7 --- /dev/null +++ b/src/models/PanelLayout.ts @@ -0,0 +1,32 @@ +export interface PanelLatest { + panelinfoid: number; + incidentid: string; + avpu: string; + avputime: number; + + airway: string; + airwaytime: number; + breathing: string; + breathingtime: number; + circulation: string; + circulationtime: number; + unconsciousness: string; + unconsciousnesstime: number; + + ecg: number; + ecgtime: number; + hr: number; + hrtime: number; + br: number; + brtime: number; + sats: number; + satstime: number; + bpsys: number; + bpdia: number; + bptime: number; + temp: number; + temptime: number; + messages: number; + messagestime: number; + deleted: number; +} diff --git a/src/models/User.ts b/src/models/User.ts new file mode 100644 index 0000000..2bd4b5d --- /dev/null +++ b/src/models/User.ts @@ -0,0 +1,7 @@ +export interface User { + id: number; + username: string; + password: string; + email:string; + _id: string; +} diff --git a/src/routes/index.ts b/src/routes/index.ts new file mode 100644 index 0000000..24b91dc --- /dev/null +++ b/src/routes/index.ts @@ -0,0 +1,62 @@ +import express, { Router } from 'express'; + +import { decode, encode } from '../middleware/jwt'; +import Checks from "../middleware/Checks"; + +import accessController from '../controllers/AccessController'; +import ecgController from '../controllers/ECGController'; +import incidentsController from '../controllers/IncidentsController'; +import incidentsV2Controller from '../controllers/IncidentsV2Controller'; + +const router: Router = express.Router(); + +router.post('/login', encode, accessController.onLogin); + +router.get('/test', decode, async (req: express.Request, res: express.Response) => { + return res.status(200).json({ + success: true, + message: 'You are good to go', + }); +}); + +// Routes + +router.get('/Ecg/GetECG', decode, ecgController.onGetECG); + +// +router.get('/Incidents/GetIncident', decode, incidentsController.onGetIncident); +router.post('/Incidents/UploadFiles', decode, incidentsController.onUploadFiles); + +// +router.post('/IncidentsV2/updatecasualty', decode, incidentsV2Controller.onUpdateCasualty); +router.post('/IncidentsV2/startincident', decode, incidentsV2Controller.onStartIncident); +router.post('/IncidentsV2/submit', decode, incidentsV2Controller.onSubmit); +router.post('/IncidentsV2/ControllerVersion', decode, incidentsV2Controller.onControllerVersion); +router.post('/IncidentsV2/getchats', decode, incidentsV2Controller.onGetChats); +router.post('/IncidentsV2/getcasualty', decode, incidentsV2Controller.onGetCasualty); +router.post('/IncidentsV2/getgroundchat', decode, incidentsV2Controller.onGetGroundChat); +router.post('/IncidentsV2/chat', decode, incidentsV2Controller.onChat); +router.post('/IncidentsV2/GetPanelInfo', decode, Checks.incidentNotNull, incidentsV2Controller.onGetPanelInfo); +router.post('/IncidentsV2/listincidents', decode, incidentsV2Controller.onListIncidents); +router.post('/IncidentsV2/listclosedincidents', decode, incidentsV2Controller.onListClosedIncidents); +router.post('/IncidentsV2/listactiveincidents', decode, incidentsV2Controller.onListActiveIncidents); +router.post('/IncidentsV2/listevents', decode, Checks.incidentExists, incidentsV2Controller.onListEvents); +router.post('/IncidentsV2/listclosedevents', decode, incidentsV2Controller.onListClosedEvents); +router.post('/IncidentsV2/acknowledgeair', decode, Checks.incidentExists, Checks.alreadyAcknowledged, Checks.validTimestamp, incidentsV2Controller.onAcknowledgeAir); +router.post('/IncidentsV2/acknowledgeground', decode, Checks.incidentExists, Checks.alreadyAcknowledged, Checks.validTimestamp, incidentsV2Controller.onAcknowledgeGround); +router.post('/IncidentsV2/typingair', decode, Checks.incidentExists, incidentsV2Controller.onTypingAir); +router.post('/IncidentsV2/typingground', decode, Checks.incidentExists, incidentsV2Controller.onTypingGround); +router.post('/IncidentsV2/nottypingground', decode, Checks.incidentExists, incidentsV2Controller.onTypingGround); +router.post('/IncidentsV2/checktypingground', decode, Checks.incidentNotNull, incidentsV2Controller.onCheckTypingGround); +router.post('/IncidentsV2/onlineair', decode, Checks.incidentExists, incidentsV2Controller.onOnlineAir); +router.post('/IncidentsV2/onlineground', decode, Checks.incidentExists, incidentsV2Controller.onOnlineGround); +router.post('/IncidentsV2/checkonlineground', decode, Checks.incidentExists, incidentsV2Controller.onCheckOnlineGround); +router.post('/IncidentsV2/checkonlineair', decode, Checks.incidentExists, incidentsV2Controller.onCheckOnlineAir); + + + + + + + +export default router; diff --git a/src/server.ts b/src/server.ts index 8bd6747..f77f967 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1,40 +1,39 @@ import * as dotenv from 'dotenv'; -import express from 'express'; +import express, { Application } from 'express'; import helmet from 'helmet'; -import cors from 'cors'; +import cors, { CorsOptions } from 'cors'; import logger from 'morgan'; -import * as jwt from 'jsonwebtoken'; - -import { authenticateToken } from './lib/authenticator'; import * as path from 'path'; -import {Login} from "./routes/login"; +import { Login } from './routes/login'; + +import indexRouter from './routes/index'; + +import { ERROR_MESSAGES } from './lib/constants'; dotenv.config(); export class App { - protected app: express.Application; + protected app: Application; - constructor(NODE_ENV: string = 'development', PORT: number = 8080) { + constructor(NODE_ENV = 'development', PORT = 8080) { const serverPort = process.env.PORT || PORT; const sitePath = 'public'; + const corsOptions: CorsOptions = { credentials: false }; this.app = express(); this.app.use(logger('dev')); this.app.use(express.json()); + this.app.use(express.urlencoded({ extended: false })); - this.app.use(cors()); + this.app.use(cors(corsOptions)); this.app.use(helmet()); this.app.set('trust proxy', 1); - this.app.post('/test', authenticateToken, (req: express.Request, res: express.Response) => { - res.sendStatus(200); - }); - // /// const login = Login(this.app); @@ -43,6 +42,15 @@ export class App { res.sendFile('index.html', { root: path.join(process.cwd(), sitePath) }); }); + this.app.use(`${process.env.URL_PREFIX}`, indexRouter); + + this.app.use('*', (req, res) => { + return res.status(404).json({ + success: false, + message: ERROR_MESSAGES.ENDPOINT_NOT_FOUND, + }); + }); + this.app.listen(serverPort, function () { console.log('The server is running in port localhost: ', serverPort); });