1.1.3 • Published 4 years ago

pilar-server v1.1.3

Weekly downloads
-
License
MIT
Repository
-
Last release
4 years ago

Pilar

A super lightweight framework for working with express in a more structural and easy way.

How to use it

In order to get started using Pilar just run

$ yarn add pilar-server

Then in order to create a new express server you add the following

const pilar = new Pilar({
  port: 3001,
  cors: true,
  baseRoute: '/',
  routes: []
});
pilar.listen();

With the command abov you wil get a express server listening on port 3001, but since there are no routes there will be little action on the server. Lets add a new route!

Add routes

The routes are classes using some base elements of Pilar. in order to create a new route simply create a class like this

import {Request, Response} from "express";
import {BaseRouter, ErrorResponse, IBaseRouter, SuccessResponse} from "pilar-server";

/**
* We extend the class from pilars BaseRouter. This lets us use some built in stuff like
* pilars routing system.
*/

class UserRoutes extends BaseRouter implements IBaseRouter {
    public readonly path = '/user';
    
    constructor() {
        super();
        this.initRoutes();
    }
    
    /**
    * This is were you setup all your routes. The works like a normal express route
    * and can have middlewares. 
    */
    initRoutes(): any {
        this.router.get(`${this.path}`, ifYouLikeMiddleWarePlaceItHere,  UserRoutes.getUser);
    }
    
    private static async getUser(req: Request, res: Response) {
        res.send(new SuccessResponse(200, {user: "some user data"}));
    }
}
export class UserRoutes;

Then wen you have setup a route simply add it to your pilar class like so:

const pilar = new Pilar({
  port: 3001,
  cors: true,
  baseRoute: '/',
  routes: [new UserRoutes()]
});

if you want middlewares that are used on all routes you can add them here:

const pilar = new Pilar({
  port: 3001,
  cors: true,
  baseRoute: '/',
  middleWares: [myloggerMiddleware],
  routes: [new UserRoutes()]
});

pilar also comes with a simple logger in order to turn it on simply add logging:true

const pilar = new Pilar({
  port: 3001,
  cors: true,
  baseRoute: '/',
  middleWares: [myloggerMiddleware],
  routes: [new UserRoutes()],
  logging: true
});
1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago