panther-express
v1.4.5
Published
Express based minimalist framework
Downloads
23
Readme
Using panther-express
//file = Server.ts
import PantherExpress from "panther-express";
let P = new PantherExpress();
//setup view engine
import ejs from "ejs";
P.setupViewEngine('ejs',ejs.renderFile);
//setup session and session store
import FileStore from "session-file-store";
import ExpressSession from "express-session";
P.setupSessionStore({
trust_proxy: false, saveUninitialized: true, resave: true, cookie: { secure: false },
secret: process.env.COOKIE_SECRET_KEY,
store: new (FileStore(ExpressSession))({path: "./session", ttl: 3600, reapInterval: 3600, logFn() {}})
});
//setup static files
PantherExpress.public_folders = [
["image"], // example.com/image/ => src/image folder access
["assets", "client/assets"], // example.com/assets/ => src/client/assets folder access
]
//setup router
import appRouter from "./app/router/AppRouter";
import mainRouter from "./app/router/MainRouter";
import serviceRouter from "./app/router/ServiceRouter";
P.addRouter([
{class: appRouter, location: '/'},
{class: adminRouter, location: '/admin'},
{class: serviceRouter, location: '/service'},
]);
P.StartServer(3000,true); // (port: number,compress: boolen)
Create App Router
//File = router/AppRouter.ts
import {RouterController, RoutesType} from "panther-express";
import controllerPageIndex from "../controller/page/website/index";
class AppRouter extends RouterController{
constructor() {
super();
this.init();
super.initialize();
}
init(): void {
super.setRoutes([
{page: ['/','/index.:ext?'], method: RoutesType.GET, func: controllerPageIndex.getIndex }, // example.com or example.com/index.html
{page: '/games.:ext', method: RoutesType.GET, func: controllerPageIndex.getGames }, // example.com/games or example.com/games.html
{page: '/games/:name', method: RoutesType.GET, func: controllerPageIndex.getGamesByName }, // example.com/games/:name
])
}
}
export default AppRouter;
Create Service Router
//File = router/ServiceRouter.ts
import {RouterController, RoutesType} from "panther-express";
import controllerServiceUser from "../controller/service/user";
class ServiceRouter extends RouterController{
constructor() {
super();
this.init();
super.initialize();
}
init(): void {
super.setRoutes([
{page: '/login', method: RoutesType.GET, func: controllerServiceUser.getUser },
])
}
}
export default ServiceRouter;
Create Page Controller
//File = controller/page/website/index.ts
import Express from "express";
import {PageController} from "panther-express";
const PAGE = {view_folder: 'website', title: 'Anasayfa', links: '', scripts: ''}
class controllerPageIndex extends PageController{
static getIndex(req: Express.Request,res: any) { // example.com or example.com/index.html
super.PageCreator(res,{
view_folder: PAGE.view_folder,
params: {
title: PAGE.title,
data: {slider: { class_type: 'is-fullheight'}},
sections: [{url:'index'}]
}
})
}
static getGames(req: Express.Request,res: any) { // example.com/games or example.com/games.html
super.PageCreator(res,{
view_folder: PAGE.view_folder,
params: {
title: PAGE.title,
data: {games: [{name: 'football',image: 'foobal.jpg',content: 'bla bla bla'}]},
sections: [{url:'games'}]
}
})
}
static getGamesByName(req: Express.Request,res: any) { // example.com/games/:name
let game = req.params.name;
let [row,data] = sql.query(`select * from ${game}`) //example query
super.PageCreator(res,{
view_folder: PAGE.view_folder,
params: { //send ejs templates
title: req.params.name,
data: {
game: row
},
sections: [{url:'games'}]
}
})
}
}
export default controllerPageIndex;
Create Service Controller
//File = controller/page/service/user.ts
import Express from "express";
class controllerServiceUser {
static getUser(req: Express.Request,res: Express.Response) {
res.status(200).json({name: 'akif', email: '[email protected]'})
}
}
export default controllerServiceUser;
##Page Skeleton description coming soon