1.0.0 • Published 4 years ago

@jrh-works/route v1.0.0

Weekly downloads
-
License
-
Repository
github
Last release
4 years ago

@jrh-works/route

Basic HTTP request and response handling for Node.js.

For a quickstart, see the usage example.

Installation

npm install @jrh-works/route

The Routing Configuration Function

Usage

const { configureRoute } = require('@jrh-works/route')

Syntax

configureRoute(options)

Arguments

NameTypeDescription
optionsObject: OptionsConfiguration options for the handling function.

Returns

TypeDescription
Function: RouteA configured route function.

Exceptions

Throws a standard Error if:


The Options Object

AttributeTypeDescription
logFunction: LogA log function which will be used to log request and response information.

The Log Function

A function for logging information.

The function should include the properties .error() for logging errors and .request() for logging from an HTTP Request object.

Example Syntax
function log(value) {
  console.log(value)
}

log.error = (error) => {
  console.error(error)
}

log.request = (request) => {
  console.log(request.method, request.url)
}

The Route Function

Syntax

route(handler)

Arguments

NameTypeDescription
handlerFunction: HandlerA handler function to execute.

Returns

No return value.

Exceptions

Throws a standard Error if:

  • A handler function is not present.

Effects

  • Information about the request and response will be logged using the log function.

The Handler Function

Syntax
handle(request, response)

Arguments

NameTypeDescription
requestObject: HTTP RequestA standard HTTP Request object.
responseObject: HTTP ResponseA standard HTTP Response object.

Returns

TypeDescription
Number | Object: RouteResponseThe value which will determine the HTTP response.

Returning a Number

If the handler returns a number, the route will respond with that status (i.e. 200). To include a JSON response in addition to a status, return a RouteResponse.

Exceptions

If the handler throws a standard error:

  • The error will be logged using the log function.
  • The route will respond with a 500 status.

If the handler throws a handler error:

  • The route will respond with the status in the handler error.

The RouteResponse Object
AttributeTypeDescription
statusNumberAn HTTP status code to send.
dataObject | StringData to send as a JSON response body.

The Handler Error

The handler error can be thrown from within a handler function to customize the error status.

Usage

const { handlerError } = require('@jrh-works/route')

Syntax
handlerError(message, options)
Arguments
NameTypeDescription
messageStringAn error message.
optionsObject: ErrorOptionsConfiguration for the handler error.

The ErrorOptions Object
AttributeTypeDescription
statusNumberAn HTTP error status code.

Usage Example

const { configureRoute, handlerError } = require('@jrh-works/route')
const log = require('./my-logger')

const route = configureRoute({ log })

const handler = (request, response) => {
  if (request.header('Authorization') !== process.env.API_KEY) {
    throw handlerError('Unauthorized request.', { status: 401 })
  }

  return 200
}

module.exports = route(handler)
0.1.0

4 years ago

1.0.0

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago