UNPKG

simple-json-server

Version:
1 lines 1.43 kB
const start=(e,o)=>{const s=require("express"),r=require("fs"),n=require("body-parser");let l=require(o);const t=s();t.use(n.urlencoded({extended:!1})),t.use(n.json());const a=e||3e3;r.watchFile("./db.json",(e,s)=>{console.log("DB File Changed --\x3e Reloading ----\x3e");const n=r.readFileSync(o);l=JSON.parse(n),console.log("Reloaded ---\x3e")}),t.all("*",(e,o,s)=>{console.log("\n\n\n\n\n\n----------------------------------New Request\t-------------------------------------"),console.log(`----------Headers ---------- \n${JSON.stringify(e.headers).split(",").join("\n")}`),console.log(`----------Body ------------ \n${JSON.stringify(e.body)}`),o.header("Access-Control-Allow-Origin","*"),o.header("Access-Control-Allow-Methods","PUT, GET, POST, DELETE, OPTIONS"),o.header("Access-Control-Allow-Headers","Content-Type"),o.header("Access-Control-Allow-Credentials","true"),o.header("Access-Control-Allow-Headers","access-control-allow-origin, authorization, content-type, x-auth-token, x-request-id, x-userid"),o.header("Access-Control-Max-Age","1800"),o.header("Allow","GET, HEAD, POST, PUT, DELETE, OPTIONS, PATCH"),o.header("Vary","Origin"),s()}),t.all("/:route",(e,o)=>{console.log(`----------URL ---------- \n${e.params.route}`),o.send(l[e.params.route][e.method]||l[e.params.route])}),t.listen(a,e=>{if(e)return console.log("something bad happened",e);console.log(`server is listening on ${a}`)})};exports.start=start;