Installation
npm install @express-love/functional-responses
Usage
send
Sends a specified response using the specified express res
object.
Parameters
Examples
const express = require('express');
const functionalMiddleware = require('@express-love/functional-middleware');
const responses = require('@express-love/functional-responses');
const apiHandler = createResponse => functionalMiddleware({
createResponse,
sendResponse: responses.send,
});
const app = express();
app.get('/api/examples', apiHandler(async (req) => {
return responses.ok('hello world');
}));
ok
Returns a 200 response.
Parameters
Returns Object A response object.
created
Returns a 201 response.
Parameters
Returns Object A response object.
badRequest
Returns a 400 response.
Parameters
Returns Object A response object.
unauthorized
Returns a 401 response.
Returns Object A response object.
forbidden
Returns a 403 response.
Returns Object A response object.
notFound
Returns a 404 response.
Returns Object A response object.
setHeaders
Adds headers to a response. This function returns a new object.
Parameters
Examples
const responses = require('@express-love/functional-responses');
const createResponse = async (req) => {
return responses.setHeaders(
responses.ok('{}'),
{ 'Content-Type': 'application/json' },
);
}
Returns Object A response object.