A general HTTP Response class for LMOS for Node.js project
- Sending HTTP responses
- Sending LMError and other Errors as HTTP responses
- Node.js 20
- Express.js 4
npm install @leismore/lmos-nodejs-lmresponse
npm test
npm run build
import { LMResponse, LMResponseData } from '@leismore/lmos-nodejs-lmresponse';
// In Express.js routing handler
function(req, res, next)
{
const resp = new LMResponse(res);
let data:LMResponseData = {
statusCode: '200',
headers: { 'Content-Type': 'application/json' },
body: { 'result': 'OK' }
};
resp.send(data);
}
// In Express.js error handler
function(error, req, res, next)
{
const resp = new LMResponse(res);
resp.sendERROR(error);
}
type LMResponseData = { // HTTP response
readonly statusCode: string, // HTTP response status code
headers?: {readonly [key:string]: string}, // HTTP headers
body?: any // HTTP body
};
/**
* LMResponseError
* 1 invalid_http_statusCode
* 2 invalid_http_header
* 3 invalid_http_body
*/
class LMResponseError extends LMError
{
public constructor(message:string, code:string, previous?:Error)
}
Refer to LMError
import {Response as ResExpress} from 'express';
protected readonly res:ResExpress;
import {Response as ResExpress} from 'express';
public constructor(res:ResExpress)
/**
* Send HTTP response
* @param {LMResponseData} response - HTTP response data
* @throws {LMResponseError} - 1/2/3
*/
public send(response:LMResponseData):void
/**
* Server-side error -> Stop this whole application
*
* Exit Code: 1
*/
public sendERROR(error:Error):void
Refer to LMError
© Leismore 2024
GNU AFFERO GENERAL PUBLIC LICENSE v3
- Kyle Chine since 04 Sep 2019
- @leismore/response (Obsoleted)
Product of Leismore OpenSource
Leismore (Australian Business Number: 25 935 862 619) is your affordable and reliable business software provider since 2021