2.1.5 • Published 2 years ago

express-lite-router v2.1.5

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

express-lite-router

Installation

npm i express-lite-router

Usage

import {Router} from "express-lite-router";
import express, {Application} from "express";
import mariadb from "mariadb";

const router = Router({
    router: express.Router(),
    dir: 'src/backend',
    context: {
        pool: mariadb.createPool({
            host: 'mydb.com',
            user: 'myUser',
            password: 'myPassword',
            connectionLimit: 5
        })
    }
});

router.get('/', 'Home/HomeController@index');

const app: Application = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(router.express());
app.listen(3000, () => console.log('Development server http://localhost:3000 started'));

Creating controller

Your controller must be extended from 'Controller'

import {Controller} from "express-lite-router";
import Model from "@backend/Home/HomeModel";

export default class HomeController extends Controller {
    private model: Model;

    public async init() {
        this.model = await this.$create(Model, 1);
    }

    public async index() {
        try {
            return this.success({rows: await this.model.select()});
        } catch (e) {
            return this.error(e);
        }
    }
}

Creating Model

Your model must be extended from 'Model'

import {Model} from "express-lite-router";
import {Pool} from "mariadb";

export default class HomeModel extends Model {
    private id;

    public constructor(id: number) {
        super();

        this.id = id;
    }

    public async init() {
        console.log("Hello, I'm a webhook");
    }

    public async select() {
        return await this.pool.query(`SELECT ${this.id} as val`);
    }

    private get pool(): Pool {
        return this.$context.pool;
    }
}

Set 'lang' app

// OR http://localhost:3000/api?lang=ua
// OR { lang: 'ua' }

console.log(this.$lang); // 'ua'

Can use 'request'

console.log(this.request); // express Request

Can use 'response'

this.response; // express Response

Can use '$context'

const router = Router({
    context: {
        pool: mariadb.createPool({
            host: 'mydb.com',
            user: 'myUser',
            password: 'myPassword',
            connectionLimit: 5
        })
    }
});

console.log(this.$context); // { pool, request, response }

Can use '$lang'

console.log(this.$lang); // 'en'

Can use '$l(message)'

console.log(this.$l({"en":"Not found", "ua":"Не знайдено"})); // 'Not found'

Can use '$create(Class, ...params)'

this.model = await this.$create(Model, 1);

Can use 'success(data? = {}, status? = 200)'

try {
    return this.success(); // json
} catch (e) {
    
}

Can use 'error(e)'

try {
    
} catch (e) {
    return this.error(e); // json
}

Can use 'ApiError(message, status? = 400, code? = 0)'

try {
    throw new ApiError('Not found', 404);
} catch (e) {
    return this.error(e); // json
}
2.1.4

2 years ago

2.1.5

2 years ago

2.1.3

2 years ago

2.1.2

2 years ago

2.1.1

2 years ago

2.1.0

2 years ago

2.0.0

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago