als-http-error

1.0.1 • Public • Published

Documentation

Overview

This library provides tools for handling HTTP errors in Node.js applications. It includes a set of pre-defined error codes, a template for generating error HTML pages, and a function for generating HTTP error responses.

Installation

Install the package using npm:

npm install als-http-error

Usage

Error Codes

A map of standard HTTP error codes to their messages.

Example

const { errorsCodes } = require('als-http-error');

console.log(errorsCodes.get(404)); // Outputs: 'Not Found'

Error Template

A function that returns an HTML string representing an error page.

Example

const { errorTemplate } = require('als-http-error');

const errorPage = errorTemplate(404, 'Not Found');
console.log(errorPage); // Outputs HTML for 404 error page

Generating HTTP Error Responses

A function that generates a function for handling HTTP errors.

Example

const http = require('http');
const { genHttpErrorFn, errorTemplate } = require('als-http-error');

const httpErrorHandler = genHttpErrorFn(errorTemplate);

const server = http.createServer((req, res) => {
   if (req.url === '/error') {
      httpErrorHandler(res, 404, 'Not Found');
   } else {
      res.end('Hello World!');
   }
});

server.listen(3000, () => {
   console.log('Server running on port 3000');
});

In this example, when you visit http://localhost:3000/error, you will receive a 404 error page.

API Reference

errorsCodes

  • A Map of HTTP error codes to error messages.
  • Use get(code) to retrieve an error message for a specific code.

errorTemplate(code, message)

  • Generates an HTML page for an error.
  • code: HTTP status code (number).
  • message: Error message (string).

genHttpErrorFn(template, defaultCode)

  • Returns a function to handle HTTP errors.
  • template: A function that generates error pages (like errorTemplate).
  • defaultCode: Default error code if none is provided (defaults to 500).

httpErrorHandler

  • A pre-configured error handler using errorTemplate.

Readme

Keywords

none

Package Sidebar

Install

npm i als-http-error

Weekly Downloads

3

Version

1.0.1

License

ISC

Unpacked Size

9.87 kB

Total Files

8

Last publish

Collaborators

  • alexsorkin