UNPKG

vaxe

Version:

JSON structured automatic route package with additions to improve usability

127 lines (122 loc) 3.78 kB
const express = require("express") const fs = require("fs") const helmet = require("helmet") const prompt = require("prompt-sync")() const morgan = require("morgan") const app = express() class route { constructor() { } setup(data, port) { if (data.constructor.name == "Object") { data = data } else if (data.constructor.name == "String") { if (fs.existsSync(data) && data.endsWith(".json")) { data = JSON.parse(fs.readFileSync(data)) } else { console.error("Error", "Invalid data") return; } } else { console.error("Error", "Invalid data") return; } if (data["secure"]) { app.use(helmet()) } if (data["logger"]) { app.use(margan("dev")) } app.use((req, res) => { function cmd(content) { let ip = req.ip.split(":")[3] return content.replaceAll("$ip", ip).replaceAll("$path", req.path) } function use(name) { if (data[name]["type"] == "content") { if (!!data[req.path]["cmd"] && data[req.path]["cmd"] == "true") { data[req.path]["value"] = cmd(data[req.path]["value"]) } res.send(data[name]["value"]) if (!!data[name]["status"] && data[name]["status"].constructor.name == 'Number'){ res.status(data[name]["status"]) } } else if (data[name]["type"] == "file") { if (fs.existsSync(data[name]["value"])) { req.sendFile(__dirname + data[name]["value"]) if (!!data[name]["status"] && data[name]["status"].constructor.name == 'Number'){ res.status(data[name]["status"]) } } else { console.error("Error", "file not exist") return; } } else if (data[name]["type"] == "link") { res.redirect(data[name]["value"]) } } if (!!data[req.path]) { if (!!data[req.path]["type"] && !!data[req.path]["value"]) { use(req.path) } } else if (!!data["404"]) { if (!!data[req.path]["type"] && !!data[req.path]["value"]) { use(req.path) } } if (!!data[req.path]["console"]) { if (!!data[req.path]["cmd"] && data[req.path]["cmd"] == "true") { data[req.path]["console"] = cmd(data[req.path]["console"]) } console.log(data[req.path]["console"]) } }) if (!!port) { app.listen(port) } } port(port) { app.listen(port) } static(folder) { if (!fs.existsSync("./" + folder)) return console.log("err") app.use(express.static(folder)) } } exports.route = new route() exports.input = function(value = "") { return prompt(value) } exports.print = function(value = "") { process.stdout.write(value) } exports.printIn = function(value = "") { process.stdout.write(value + "\n") } exports.random = function(val) { if (val.constructor.name == "Number") return Math.floor(Math.random() * val) if (val.constructor.name == "Array") return val[Math.floor(Math.random() * val.length)] } exports.generate = function(type, num = 10) { const types = {strL:["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"],strU:["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"],num:["1","2","3","4","5","6","7","8","9","0"]} let a = "" switch (type) { case "str": a = types.strL.concat(types.strU) break; case "strLow": a = types["strL"] break; case "strUp": a = types["strU"] break; case "num": a = types["num"] break; } let ret = "" for (let i = 0;i < num;i++) { ret += a[Math.floor(Math.random() * a.length)] } return ret }