Skip to content

Instantly share code, notes, and snippets.

@ganeshkbhat
Last active September 19, 2022 05:25
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/4f79cc055738e185745ba28217f8e41c to your computer and use it in GitHub Desktop.
Save ganeshkbhat/4f79cc055738e185745ba28217f8e41c to your computer and use it in GitHub Desktop.
Expressjs: API Template Gulp Automation
/**
* ExpressJS Template Based Routes
Path: ./
*/
const path = require('path');
const fs = require('fs');
var app = require("express");
var normalizedPath = require("path").join(__dirname, "routes");
async function requireFiles(d, r = false, cb = (f) => { app.use(require(path.join(f, "index.js"))); }) {
const dir = await fs.promises.opendir(d);
var f;
while (f = dir.readSync()) {
if (!f.isFile()) {
cb(path.join(d, f.name));
}
}
dir.close();
}
requireFiles('./').catch(console.error);
app.all("*", function(req, res, next) {
res.send("Hello World Route Present");
})
app.listen(8000, function() {
console.log("Server started at 8000");
});
/**
* ExpressJS Template Based Routes
* Path: ./gulpfile.js
* // // INSTALL GULP
* // npm install --save-dev gulp
* // // RUN GULP COMMAND
* // ./node_modules/.bin/gulp route --name routename
*/
const { src, dest } = require("gulp");
function route(cb, name) {
const arg = (argList => {
let arg = {}, a, opt, thisOpt, curOpt;
for (a = 0; a < argList.length; a++) {
thisOpt = argList[a].trim();
opt = thisOpt.replace(/^\-+/, '');
if (opt === thisOpt) {
if (curOpt) arg[curOpt] = opt;
curOpt = null;
}
else {
curOpt = opt;
arg[curOpt] = true;
}
}
return arg;
})(process.argv);
src('./templates/*.*')
.pipe(dest("routes/" + arg.name + "/"));
cb();
}
exports.route = route;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment