0.1.0 • Published 6 years ago

@express-love/functional-middleware v0.1.0

Weekly downloads
-
License
ISC
Repository
github
Last release
6 years ago

Installation

npm install @express-love/functional-middleware

Usage

functionalMiddleware

Creates an express middleware function that allows endpoints to be expressed as a function returning a response value. The specified createResponse function will be called and the response value that is returned will be sent to the specified sendResponse function.

Parameters

  • options Object
    • options.createResponse Function An async function that creates a response object.
    • options.sendResponse Function A function that will send the created response object.

Examples

const express = require('express');
const functionalMiddleware = require('@express-love/functional-middleware');

const sendResponse = (res, response) => res.status(response.status).send(response.body);
const apiHandler = (createResponse) => functionalMiddleware({ createResponse, sendResponse });

const app = express();

app.get('/api/examples', apiHandler(async (req) => {
  await doSomething();
  return { status: 200, body: 'hello world' };
}));

Returns Function An express middleware function.