Skip to content

Middlewares

The zely is based on the osik aimed at light weight and speed. Reading the osik documentation will help you understand.

Usage

ts
import { Middleware } from 'zely';

export const Message: Middleware = (req, res, next) => {
  (req as any).message = 'Hello World!';
  next();
};
import { Middleware } from 'zely';

export const Message: Middleware = (req, res, next) => {
  (req as any).message = 'Hello World!';
  next();
};
ts
import { ZelyRequest, ZelyResponse } from 'zely';

export function get(req: ZelyRequest, res: ZelyResponse) {
  res.end((req as any).message);
}
import { ZelyRequest, ZelyResponse } from 'zely';

export function get(req: ZelyRequest, res: ZelyResponse) {
  res.end((req as any).message);
}

Regists

Add them to config.middlewares.

ts
import { Message } from './middlewares/message';

export default defineConfig({
  middlewares: [Message],
  // ...
});
import { Message } from './middlewares/message';

export default defineConfig({
  middlewares: [Message],
  // ...
});

Auto Mode experimental

The auto mode is a feature that loads all files in the middlewares directory and applies them as middleware.
It has the advantage of not having to change settings to add middleware, but unnecessary files are loaded, which can cause slowdowns.

To use auto middleware mode, edit config value.

ts
import { Middleware } from 'zely';

const Message: Middleware = (req, res, next) => {
  (req as any).message = 'Hello World!';

  next();
};

// export as default

export default Message;
import { Middleware } from 'zely';

const Message: Middleware = (req, res, next) => {
  (req as any).message = 'Hello World!';

  next();
};

// export as default

export default Message;

The important point here is that middleware is exported as default.

ts
export default defineConfig({
  allowAutoMiddlewares: true,
  middlewareDirectory: 'middlewares',
});
export default defineConfig({
  allowAutoMiddlewares: true,
  middlewareDirectory: 'middlewares',
});