yasui

Light framework using Express for Node.js applications.

Usage no npm install needed!

<script type="module">
  import yasui from 'https://cdn.skypack.dev/yasui';
</script>

README

Yasui

Light framework using Express for Node.js applications.

(◠‿◠)やすいです!

Yasui can mean "easy" in Japanese. Yasui is meant to be easy to use, light, going to essentials only, with few dependencies. Yasui simplifies your life by providing you tools to quickly implement your controllers, middleware, endpoints, and your server. Yasui provides also complete errors management (logs and client responses) and logging service.

 

Get started

$ npm install yasui
import yasui from 'yasui';
yasui.createServer({ });

createServer will create an http server and listen it. Use yasui.createApp({ }), that return an express application, if you want to perform additional operations on it before listening the server and use your own listening method.

Browse the src/examples folder to get an example of a simple server using the basic features of Yasui.

 

Configuration

createServer and createApp takes a configuration object with the following parameters:

Parameter Description
controllers An array containing your controllers (classes using the @Controller decorator).
middlewares An array containing your application level middlewares (classes using the @Middleware decorator).
environment The name of your environment.
port The listening port of your server (not needed in createApp). 3000 by default
debug Boolean, display logs more provided if true, and logs all incoming requests.
apiKey Authentication key for your API. If provided, all requests should contain the x-api-key header.

 

Controllers

Yasui provides decorators to define your controllers and endpoints.

The Controller decorator takes in parameter the root path of its endpoints.

The methods of your controller can be decorated with the following: @Get, @Post, @Put, @Delete, @Patch. These take in parameter the relative path of the endpoint.

The parameters of your endpoint can be decorated with the following: @Res, @Req, @Next, to reflect express arguments, or with @Param, @Body, @Query, @Header to select a specific parameter from the query, or a subset of it ; they can take the name of the desired parameter as a parameter, in which case they will return the whole set.

@Logger also provides a query-specific timed logger service.

Example

import express from 'express';
import { Get, Controller, Res, Param } from 'yasui';

@Controller('/')
export class MyController {

    @Get('/:name')
    private hello(
        @Param('name') name: string,
        @Res() res: express.Response
    ): void {
        res.status(200).json({ message: `Hello ${name}!` });
    }
}

 

Middlewares

Yasui provides also decorators to define your middlewares like controllers and use its at application, controller, or endpoint level.

The Middleware decorator does not take parameter.

The parameters of your middleware can be decorated with the same decorators as controller endpoints.

Your middleware must obligatorily implement an use() method, which will define the execution function of it.

Example

import express from 'express';
import { logger, Middleware, Param, Next } from '..';

@Middleware()
export class HelloMiddleware {
    use(
        @Param('name') name: string,
        @Next() next: express.NextFunction
    ): void {
        logger.log(`Hello ${name}`);
        next();
    }
}
@Controller('/', HelloMiddleware)
// [...]
@Get('/:name', HelloMiddleware)
// [...]

 

Dependency injections

Not yet documented.

See src/examples folder for examples.