From 160aaca468b89ce8002168b738d0f5d77ce2e1ef Mon Sep 17 00:00:00 2001 From: Kato Twofold Date: Sat, 22 May 2021 23:12:00 +0300 Subject: [PATCH] + Added dotenv + The actual boilerplate --- .env | 2 ++ README.md | 14 +++++++++ package-lock.json | 1 - package.json | 3 +- src/app.ts | 63 +++++++++++++++++++++++++++++++++++++++ src/config/config.ts | 22 ++++++++++++++ src/controllers/Sample.ts | 16 ++++++++++ src/routes/Sample.ts | 8 +++++ 8 files changed, 126 insertions(+), 3 deletions(-) create mode 100644 .env create mode 100644 README.md create mode 100644 src/config/config.ts create mode 100644 src/controllers/Sample.ts create mode 100644 src/routes/Sample.ts diff --git a/.env b/.env new file mode 100644 index 0000000..53b65c6 --- /dev/null +++ b/.env @@ -0,0 +1,2 @@ +SERVER_PORT=6950 +SERVER_HOST=0.0.0.0 \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..f5f3a19 --- /dev/null +++ b/README.md @@ -0,0 +1,14 @@ +## Description +A boilerplate for `ExpressJS` using `TypeScript` + +## Dev Running +```bash +# Run the following command in the root of the folder +nodemon .\src\app.ts` +``` + +## Building +```bash +# Run the following command in the root of the folder +npm build +``` diff --git a/package-lock.json b/package-lock.json index 48a70da..80c359c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,6 @@ "version": "1.0.0", "license": "ISC", "dependencies": { - "body-parser": "^1.19.0", "dotenv": "^10.0.0", "express": "^4.17.1", "npm": "^7.14.0" diff --git a/package.json b/package.json index 31ab215..1b3c540 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,10 @@ { "dependencies": { - "body-parser": "^1.19.0", "dotenv": "^10.0.0", "express": "^4.17.1", "npm": "^7.14.0" }, - "name": "shopperaserver2", + "name": "boilerplate", "version": "1.0.0", "main": "./src/app.js", "devDependencies": { diff --git a/src/app.ts b/src/app.ts index e69de29..d0b06c3 100644 --- a/src/app.ts +++ b/src/app.ts @@ -0,0 +1,63 @@ +// #region Imports + import http from 'http'; + import express from 'express'; + import config from './config/config'; + + // Routes + import sampleRoutes from './routes/Sample'; + +//#endregion + +const NAMESPACE = `🦝`; +const router = express(); + + +router.use((req, res, next) => { + // Log the request to the server + console.info(NAMESPACE, `METHOD: [${req.method}], URL: [${req.url}], IP: [${req.socket.remoteAddress}]`); + + res.on(`finish`, () => { + console.info(NAMESPACE, `METHOD: [${req.method}], URL: [${req.url}], IP: [${req.socket.remoteAddress}], STATUS: ${res.statusCode}`); + }) + + next(); + +}) + +// Parse the request +router.use(express.urlencoded({ extended: false })); +router.use(express.json({strict: false})); + +// API Rules +router.use((req, res, next) => { + res.header(`Access-Control-Allow-Origin`, `*`); + res.header(`Access-Control-Allow-Headers`, `Origin, X-Request-With, Content-Type, Accept, Authorization`); + + if ( req.method == 'OPTIONS' ) { + res.header(`Access-Control-Allow-Methods`, 'GET PATCH DELETE POST PUT'); + return res.send(200).json({}); + } + + next(); +}) + +// Routing + +router.use('/sample', sampleRoutes); + +{ // Error handling + router.use((req, res, next) => { + const error = new Error(`Api Route Not Found`); + + return res.status(404).json({ + message: error.message + }) + + }); +} + +const httpServer = http.createServer(router); +httpServer.listen(config.server.port, () => { + console.info(NAMESPACE, `Server Running on Port ${config.server.hostname}:${config.server.port}`) +}) + diff --git a/src/config/config.ts b/src/config/config.ts new file mode 100644 index 0000000..cf340a7 --- /dev/null +++ b/src/config/config.ts @@ -0,0 +1,22 @@ +// Imports +import dotenv from "dotenv"; + +// Load in the dotconfig configuration +dotenv.config(); + +// The server's port +const SERVER_HOSTNAME : string = process.env.SERVER_HOSTNAME || '0.0.0.0'; +const SERVER_PORT : string = process.env.SERVER_PORT || '6990'; + +// Server configuration +const SERVER = { + hostname: SERVER_HOSTNAME, + port: SERVER_PORT, +}; + +// The whole configuration +const config = { + server: SERVER +}; + +export default config; \ No newline at end of file diff --git a/src/controllers/Sample.ts b/src/controllers/Sample.ts new file mode 100644 index 0000000..93a70fc --- /dev/null +++ b/src/controllers/Sample.ts @@ -0,0 +1,16 @@ +import { Request, Response, NextFunction } from 'express'; + +const NAMESPACE = `Sample Controller`; + +/** + * /ping + */ +const sampleHealthCheck = (req : Request, res : Response, next: NextFunction ) => { + console.info(NAMESPACE, `CheckHealth Called`); + + res.status(200).json({ + message: "Pong" + }); +} + +export default { sampleHealthCheck }; \ No newline at end of file diff --git a/src/routes/Sample.ts b/src/routes/Sample.ts new file mode 100644 index 0000000..fea166b --- /dev/null +++ b/src/routes/Sample.ts @@ -0,0 +1,8 @@ +import express from 'express'; +import controller from '../controllers/Sample'; + +const router = express.Router(); + +router.get("/ping", controller.sampleHealthCheck); + +export = router; \ No newline at end of file