Pipes
A Pipe
is a function to reuse common logic in your app or break down big logic into small pieces of codes.
Recommendation
Learn about Application and Actions before to read this section.
#
Defining a Pipe// log-headers.pipe.tsimport { Pipe, Ctx } from 'wind-waker';
export const logHeadersPipe: Pipe = (ctx: Ctx) => { console.log(ctx.req.headers);}
#
Using the PipeA pipe can be used at 3 levels:
- Application
- Group Actions
- Single Action
#
Application LevelUsing the pipe at Application level means every single request will execute this pipe. See how to set the pipe in the example below.
import { App } from 'wind-waker';import { logHeadersPipe } from './pipes/log-headers.pipe';
// Create your applicationconst app = new App();
// Set the pipe at the app levelapp.pipeline(logHeadersPipe);
// Start listen requestsapp.listen(4000);
The pipeline
function can accept multiple pipes separated by a comma.
// Set multiple pipes at the app levelapp.pipeline(logHeadersPipe, anotherPipe, ...);
Pipeline order matter
The pipes register in the pipeline will be executed in the exactly same order you pass in.
#
Available Pipes- cors
- helmet
- parse
- jwt
#
Using cors Pipelet's set for example the cors
Pipe, your app.ts
now will look like this:
import {App, cors} from 'wind-waker'
// Create a new wind-waker server by instance the App classconst app = new App();
// Setting cors Pipe at App level.// cors Pipe will enable CORS for all requests.app.pipeline(cors())
// Start listen to incoming request on port 4000app.listen(4000);