Skip to content

Instantly share code, notes, and snippets.

@ganeshkbhat
Last active May 10, 2022 11:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ganeshkbhat/d6f1d716cab745b35754812333f24dea to your computer and use it in GitHub Desktop.
Save ganeshkbhat/d6f1d716cab745b35754812333f24dea to your computer and use it in GitHub Desktop.
Automated Templatized Handlers
{
"host": "127.0.0.1",
"port": 11010,
"static": [
{
"path": "/web/path",
"folderpath": "/path/to/folder"
},
{
"folderpath": "/path/to/folder"
}
],
"middlewares": [
{
"path": "/web/path",
"middlewares": ["testMiddleware"]
},
{
"middlewares": ["testMiddleware"]
}
],
"set": [
{
"name": "view engine",
"value" : "pug",
"type": "functionORstringORarray",
"invoke": true
}
],
"paths": [
{
"method": "get",
"path": "/test",
"function": "testGetHandler",
"middlewares": ["testMiddleware"]
}
]
}
const path = require('path');
const express = require('express');
let app = express();
// Require dynamically based on need
const configPath = "./config.json";
const config = require(configPath);
let routes = config.paths;
let configPathLen = routes.length;
let configStaticLength = config.static.length;
let configMiddlewaresLength = config.middlewares.length;
let configSetLen = config.set.length;
let anyPathHandled = false;
function testGetHandler(req, res, next) {
res.send("Passed test handler");
}
function testMiddleware(req, res, next) {
console.log("Test Middlewares");
next();
}
const handlerFunctions = [testGetHandler, testMiddleware];
for (let i = 0; i < configSetLen; i++) {
app.set(config.set[i].name, config.set[i].value);
}
for (let i=0; i< configStaticLength; i++) {
if (!!config.static[i].path) {
app.use(config.static[i].path, express.static(path.join(__dirname, config.static[i].folderpath)));
} else {
app.use(express.static(path.join(__dirname, config.static[i].folderpath)));
}
}
for (let i=0; i< configMiddlewaresLength; i++) {
let appmiddlewares = handlerFunctions.filter((item) => { if (config.middlewares[i].middlewares.includes(item.name)) { return true; } return false; });
if (!!config.static[i].path) {
app.use(config.middlewares[i].path, ...appmiddlewares);
} else {
app.use(...appmiddlewares);
}
}
for (let i = 0; i < configPathLen; i++) {
let middlewares = handlerFunctions.filter((item) => { if (routes[i].middlewares.includes(item.name)) { return true; } return false; });
let callbackHandler = handlerFunctions.filter((item) => { if (item.name === routes[i].function) { return true } return false; });
if (!routes[i].path || !callbackHandler) {
console.log("routes[i].path, handlerFunctions[routes[i].function] Error: ", routes[i].path, ...middlewares, ...callbackHandler)
process.exit(1);
}
if (routes[i].method.toLowerCase() === "all") {
app.all(routes[i].path, ...callbackHandler);
} else if (routes[i].method.toLowerCase() === "get") {
app.get(routes[i].path, ...middlewares, ...callbackHandler);
} else if (routes[i].method.toLowerCase() === "post") {
app.post(routes[i].path, ...middlewares, ...callbackHandler);
} else if (routes[i].method.toLowerCase() === "put") {
app.put(routes[i].path, ...middlewares, ...callbackHandler);
} else if (routes[i].method.toLowerCase() === "patch") {
app.patch(routes[i].path, ...middlewares, ...callbackHandler);
} else if (routes[i].method.toLowerCase() === "delete") {
app.delete(routes[i].path, ...middlewares, ...callbackHandler);
} else if (routes[i].method.toLowerCase() === "options") {
app.options(routes[i].path, ...middlewares, ...callbackHandler);
}
if (routes[i].path === "*") {
anyPathHandled = true;
}
}
if (anyPathHandled === false) {
app.get('*', function (req, res, next) {
res.status(200)
.send({ status: "Server running but no such path ", time: Date.now() })
});
}
app.listen(Number(config.port), config.host, function () {
console.log("Server started at ", config.port);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment