+ Added dotenv + The actual boilerplate

This commit is contained in:
Daniel Legt 2021-05-22 23:12:00 +03:00
parent aa3ab5b10a
commit 160aaca468
8 changed files with 126 additions and 3 deletions

2
.env Normal file
View File

@ -0,0 +1,2 @@
SERVER_PORT=6950
SERVER_HOST=0.0.0.0

14
README.md Normal file
View File

@ -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
```

1
package-lock.json generated
View File

@ -9,7 +9,6 @@
"version": "1.0.0", "version": "1.0.0",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"body-parser": "^1.19.0",
"dotenv": "^10.0.0", "dotenv": "^10.0.0",
"express": "^4.17.1", "express": "^4.17.1",
"npm": "^7.14.0" "npm": "^7.14.0"

View File

@ -1,11 +1,10 @@
{ {
"dependencies": { "dependencies": {
"body-parser": "^1.19.0",
"dotenv": "^10.0.0", "dotenv": "^10.0.0",
"express": "^4.17.1", "express": "^4.17.1",
"npm": "^7.14.0" "npm": "^7.14.0"
}, },
"name": "shopperaserver2", "name": "boilerplate",
"version": "1.0.0", "version": "1.0.0",
"main": "./src/app.js", "main": "./src/app.js",
"devDependencies": { "devDependencies": {

View File

@ -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}`)
})

22
src/config/config.ts Normal file
View File

@ -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;

16
src/controllers/Sample.ts Normal file
View File

@ -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 };

8
src/routes/Sample.ts Normal file
View File

@ -0,0 +1,8 @@
import express from 'express';
import controller from '../controllers/Sample';
const router = express.Router();
router.get("/ping", controller.sampleHealthCheck);
export = router;