Rescale logoNEMO

Forward

Documentation for forward functions in NEMO

Forward functions in NEMO allow you to forward the response from one middleware function to another. This can be useful for chaining middleware functions together and creating more complex middleware logic.

Forward Function Schema

This example shows how to use the forward function in a middleware.

middleware.ts
import { forward, type MiddlewareFunctionProps } from '@rescale/nemo';
 
const firstMiddleware = async ({ request, response, forward }: MiddlewareFunctionProps) => {
  console.log('First middleware');
  forward(response);
};
 
const secondMiddleware = async ({ request }: MiddlewareFunctionProps) => {
  console.log('Second middleware');
};
 
const middlewares = {
  '/api': [firstMiddleware, secondMiddleware],
};
 
export const middleware = createMiddleware(middlewares);

Explanation

Prop: forward

Type: (response: MiddlewareReturn) => void

The forward function allows you to forward the response from one middleware function to another. This can be useful for chaining middleware functions together and creating more complex middleware logic.

Example Usage

In this example, the firstMiddleware logs a message and forwards the response to the secondMiddleware, which logs another message and returns the response.

middleware.ts
import { forward, type MiddlewareFunctionProps } from '@rescale/nemo';
 
const firstMiddleware = async ({ request, response, forward }: MiddlewareFunctionProps) => {
  console.log('First middleware');
  forward(response);
};
 
const secondMiddleware = async ({ request }: MiddlewareFunctionProps) => {
  console.log('Second middleware');
};
 
const middlewares = {
  '/api': [firstMiddleware, secondMiddleware],
};
 
export const middleware = createMiddleware(middlewares);

The forward function should be called with the response object that you want to forward to the next middleware function.

On this page