Initial Commit

+ Dockerization
+ Git Setup
+ Example Files
+ HandleBars Setup
+ Express Logic
+ Folder Structure
+ LICENSE
+ Public Folder
This commit is contained in:
2022-05-10 00:15:45 +03:00
commit 1b8292dc50
25 changed files with 3209 additions and 0 deletions
+16
View File
@@ -0,0 +1,16 @@
// Imports
import dotenv from "dotenv";
// Load in the dotconfig configuration
dotenv.config();
// The server's port
const SERVER_PORT: string = process.env.SERVER_PORT || '5386';
// The whole configuration
const config = {
serverPort: SERVER_PORT,
devmode: !!process.env.DEV_MODE,
};
export default config;
+26
View File
@@ -0,0 +1,26 @@
import { Request, Response, NextFunction, Router } from 'express';
const homepageController = ( req:Request, res:Response, next:NextFunction ) => {
// Make sure that the browser isn't caching this
res.setHeader(`Cache-Control`, `no-cache, must-revalidate`);
res.setHeader(`Pragma`, `no-cache`);
res.setHeader(`Expires`, `Sat, 26 Jul 1997 05:00:00 GMT`);
// Render the home page
res.render('home', {
// This is a simple variable
title: 'Page Title',
// A simple list example for the loops
listExample: {
0: {id: 0, name: `ZERO`},
1: {id: 1, name: `First`, disabled: true},
2: {id: 2, name: `Second`},
3: {id: 3, name: `Third`},
},
// layout: 'main', // Change this from main to your layout ( from the layouts folder ) if you so wish, it's basically the wrapper of it all
time: new Date().toLocaleDateString()
});
}
export { homepageController }
View File
View File
+12
View File
@@ -0,0 +1,12 @@
// Imports
import express from 'express';
import { homepageController } from '../Controllers/Homepage';
// Get the router
const router = express.Router();
// Set the route
router.get("/", homepageController);
// Export the reouter
export {router as homepageRouter};
+92
View File
@@ -0,0 +1,92 @@
// #region Imports
import http from 'http';
import express from 'express';
import config from './Config/config';
import { engine } from 'express-handlebars';
import { homepageRouter } from './Routes/Homepage';
//#endregion
// Declare a NameSpace constant for each file so that it's easier to identify where debug messages are coming from
const NAMESPACE = `App`;
// Setup the expressJS instance
const router = express();
// Set the view engine as Handlebars
router.engine('handlebars', engine({
extname: 'hbs',
}));
router.set('view engine', 'handlebars');
router.set('views', './Views/');
// Setup the router to log all activity that is happening
router.use((req, res, next) => {
// Log the request to the server
console.info(NAMESPACE, `METHOD: [${req.method}], URL: [${req.url}], IP: [${req.socket.remoteAddress}]`);
// Whenever we finish the request, send out a message telling us what exactly has happened to it.
res.on(`finish`, () => {
console.info(NAMESPACE, `METHOD: [${req.method}], URL: [${req.url}], IP: [${req.socket.remoteAddress}], STATUS: ${res.statusCode}`);
})
// Run the next function queued for this request
next();
})
// Parse the request
router.use(express.urlencoded({ extended: false }));
router.use(express.json({ strict: false }));
// API Rules
router.use((req, res, next) => {
// Set some basic headers
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
// Register the public folder where you can serve static/public data
router.use(`/public`, express.static('./src/public/'));
// Handle the homepage
router.get("/", homepageRouter);
{ // Error handling
router.use((req, res, next) => {
// Generate an error
const error = new Error(`Page Not Found`);
// Set the response to 404
res.status(404)
// Read the request's preffered response type ( default text/html )
if (!!req.headers.accept) {
// Check if HTML is acceptable
console.log(req.headers.accept);
if (req.headers.accept.includes(`text/html`)) {
return res.sendFile(`./views/errors/404.html`, { root: __dirname });
}
}
// If text/html is not accepted, then simply return a JSON
return res.json({
message: error.message
})
});
}
// Finally actually start the server and run it
const httpServer = http.createServer(router);
httpServer.listen(config.serverPort, () => {
console.info(NAMESPACE, `Server Running on Port ${config.serverPort}`)
})
View File
View File
View File