+ Added dotenv + The actual boilerplate
This commit is contained in:
parent
aa3ab5b10a
commit
160aaca468
|
@ -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
|
||||
```
|
|
@ -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"
|
||||
|
|
|
@ -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": {
|
||||
|
|
63
src/app.ts
63
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}`)
|
||||
})
|
||||
|
|
@ -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;
|
|
@ -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 };
|
|
@ -0,0 +1,8 @@
|
|||
import express from 'express';
|
||||
import controller from '../controllers/Sample';
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.get("/ping", controller.sampleHealthCheck);
|
||||
|
||||
export = router;
|
Loading…
Reference in New Issue