npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fwsp-server-response

v2.2.6

Published

Server Response module for use with Node / ExpressJS

Downloads

2,467

Readme

Server Response Build Status

A server response module for use with Node / ExpressJS.

The goal of this module is to standardize responses from JSON enabled servers. Each response sent contains the statusCode, statusMessage, statusDescription and result.

{
  "statusCode": 200,
  "statusMessage": "OK",
  "statusDescription": "Request succeeded without error",
  "result": {}
}

So why is this a big issue?

Because the server responses have the potential to travel between multiple chained service endpoints each of which might otherwise have its own idea of how a response should be formatted. Using this module offers multiple processes the opportunity to easily standardize on a uniform response format.

Other client, servers, and services which receive the formatted response can always depend on knowing where to retrieve the HTTP status (statusCode) and where to find the actual response body (result).

Installation

$ npm -i install fwsp-server-response

Usage

The easiest way to use server-response is to use one of the provided responders. The first parameter res is a Node HTTP response object or an ExpressJS response object. This allows serverResponse to send a response back through the provided res object.

serverResponse.sendOk(res, {
  result: {
    key: value
    key: value
  }
})

In the example above the data object sent provided is merged with the following object to build a response:

{
  "statusCode": 200,
  "statusMessage": "OK",
  "statusDescription": "Request succeeded without error",
  "result": {
    key: value
    key: value
  }
}

Note: ServerResponse automatically fills the statusCode, statusMessage and statusDescription fields based on the response member used. In the above example we're sending an HTTP_OK response using the sendOk member function.

So in the above example, the provided .result it merged in. This means that you can also overwrite the statusCode, statusMessage and statusDescription fields as well. However, it's recommended that only the statusDescription be modified in order to maintain consistency throughout a distributed system.

serverResponse.sendInvalidRequest(res, {
  statusDescription: 'The `to` field is missing from your request'
})

You can also add and override HTTP headers by providing a headers sub-object:

serverResponse.sendInvalidRequest(res, {
  headers: {
    'Content-Type': 'text/plain',
    'X-Powered-By': 'myCoolService/1.0'
  },
  result: {
    key: value
    key: value
  }
})

List of responders

enableCORS

Enable / Disable CORS support

/**
* @name enableCORS
* @summary Enable / Disable CORS support
* @param {boolean} state - true if CORS should be enabled
*/

createResponseObject

Create a data response object.

/**
* @name createResponseObject
* @summary Create a data response object.
* @description This creates a consistently formatted HTTP response. It can be used
*              with any of the server-response send methods in the data param.
* @param {number} httpCode - HTTP code (Ex. 404)
* @param {object} resultPayload - object with {result: somevalue}
* @return {object} response - object suitable for sending via HTTP
*/
createResponseObject(httpCode, resultPayload)

sendResponse

Send a server response to caller

/**
 * @name sendResponse
 * @summary Send a server response to caller.
 * @param {number} code - HTTP response code
 * @param {object} res - Node HTTP response object
 * @param {object} data - An object to send
 * @return {object} res - Returns the (res) response object when in test mode, else undefined
 */
sendResponse(code, res, data)

sendOk

Send an HTTP_OK server response to caller

/**
 * @name sendOk
 * @summary Send an HTTP_OK server response to caller.
 * @param {object} res - Node HTTP response object
 * @param {object} data - An object to send
 * @return {object} res - Returns the (res) response object when in test mode, else undefined
 */
sendOk(res, data)

sendCreated

Send an HTTP_CREATED server response to caller

/**
 * @name sendCreated
 * @summary Send an HTTP_CREATED server response to caller.
 * @param {object} res - Node HTTP response object
 * @param {object} data - An object to send
 * @return {object} res - Returns the (res) response object when in test mode, else undefined
 */
sendCreated(res, data)

sendMovedPermanently

Send an HTTP_MOVED_PERMANENTLY server response to caller

/**
 * @name sendMovedPermanently
 * @summary Send an HTTP_MOVED_PERMANENTLY server response to caller.
 * @param {object} res - Node HTTP response object
 * @param {object} data - An object to send
 * @return {object} res - Returns the (res) response object when in test mode, else undefined
 */
sendMovedPermanently(res, data)

sendNotFound

Send an HTTP_NOT_FOUND server response to caller

/**
 * @name sendNotFound
 * @summary Send an HTTP_NOT_FOUND server response to caller.
 * @param {object} res - Node HTTP response object
 * @param {object} data - An object to send
 * @return {object} res - Returns the (res) response object when in test mode, else undefined
 */
sendNotFound(res, data)

sendInvalidRequest

Send an HTTP_BAD_REQUEST server response to caller

/**
 * @name sendInvalidRequest
 * @summary Send an HTTP_BAD_REQUEST server response to caller.
 * @param {object} res - Node HTTP response object
 * @param {object} data - An object to send
 * @return {object} res - Returns the (res) response object when in test mode, else undefined
 */
sendInvalidRequest(res, data)

sendInvalidSession

Send an HTTP_BAD_REQUEST server response to caller

/**
 * @name sendInvalidSession
 * @summary Send an HTTP_BAD_REQUEST server response to caller.
 * @param {object} res - Node HTTP response object
 * @param {object} data - An object to send
 * @return {object} res - Returns the (res) response object when in test mode, else undefined
 */
sendInvalidSession(res, data)

sendInvalidUserCredentials

Send an HTTP_UNAUTHORIZED server response to caller

/**
 * @name sendInvalidUserCredentials
 * @summary Send an HTTP_UNAUTHORIZED server response to caller.
 * @param {object} res - Node HTTP response object
 * @param {object} data - An object to send
 * @return {object} res - Returns the (res) response object when in test mode, else undefined
 */
sendInvalidUserCredentials(res, data)

sendRequestFailed

Send an HTTP_REQUEST_FAILED server response to caller

/**
 * @name sendRequestFailed
 * @summary Send an HTTP_REQUEST_FAILED server response to caller.
 * @param {object} res - Node HTTP response object
 * @param {object} data - An object to send
 * @return {object} res - Returns the (res) response object when in test mode, else undefined
 */
sendRequestFailed(res, data)

sendDataConflict

Send an HTTP_CONFLICT server response to caller.

/**
 * @name sendDataConflict
 * @summary Send an HTTP_CONFLICT server response to caller.
 * @param {object} res - Node HTTP response object
 * @param {object} data - An object to send
 * @return {object} res - Returns the (res) response object when in test mode, else undefined
 */
sendDataConflict(res, data)

sendTooLarge

Send an HTTP_TOO_LARGE server response to caller

/**
 * @name sendTooLarge
 * @summary Send an HTTP_TOO_LARGE server response to caller.
 * @param {object} res - Node HTTP response object
 * @param {object} data - An object to send
 * @return {object} res - Returns the (res) response object when in test mode, else undefined
 */
sendDataConflict(res, data)

sendTooManyRequests

Send an HTTP_TOO_MANY_REQUEST server response to caller

/**
 * @name sendTooManyRequests
 * @summary Send an HTTP_TOO_MANY_REQUEST server response to caller.
 * @param {object} res - Node HTTP response object
 * @param {object} data - An object to send
 * @return {object} res - Returns the (res) response object when in test mode, else undefined
 */
sendTooManyRequests(res, data)

sendServerError

Send an HTTP_SERVER_ERROR server response to caller

/**
 * @name sendServerError
 * @summary Send an HTTP_SERVER_ERROR server response to caller.
 * @param {object} res - Node HTTP response object
 * @param {object} data - An object to send
 * @return {object} res - Returns the (res) response object when in test mode, else undefined
 */
sendServerError(res, data)

sendInternalError

Alias for sendResponseServerError

/**
 * @name sendInternalError
 * @summary Alias for sendResponseServerError
 * @param {object} res - Node HTTP response object
 * @param {object} data - An object to send
 * @return {object} res - Returns the (res) response object when in test mode, else undefined
 */
sendInternalError(res, data)

sendMethodNotImplemented

Send an HTTP_METHOD_NOT_IMPLEMENTED server response to caller

/**
 * @name sendMethodNotImplemented
 * @summary Send an HTTP_METHOD_NOT_IMPLEMENTED server response to caller.
 * @param {object} res - Node HTTP response object
 * @param {object} data - An object to send
 * @return {object} res - Returns the (res) response object when in test mode, else undefined
 */
sendMethodNotImplemented(res, data)

sendConnectionRefused

Send an HTTP_CONNECTION_REFUSED server response to caller

/**
 * @name sendConnectionRefused
 * @summary Send an HTTP_CONNECTION_REFUSED server response to caller.
 * @param {object} res - Node HTTP response object
 * @param {object} data - An object to send
 * @return {object} res - Returns the (res) response object when in test mode, else undefined
 */
sendUnavailableError(res, data)

sendUnavailableError

Send an HTTP_METHOD_NOT_IMPLEMENTED server response to caller

/**
 * @name sendUnavailableError
 * @summary Send an HTTP_METHOD_NOT_IMPLEMENTED server response to caller.
 * @param {object} res - Node HTTP response object
 * @param {object} data - An object to send
 * @return {object} res - Returns the (res) response object when in test mode, else undefined
 */
sendUnavailableError(res, data)

Exported HTTP codes

ServerResponse.HTTP_OK = 200;
ServerResponse.HTTP_CREATED = 201;
ServerResponse.HTTP_MOVED_PERMANENTLY = 301;
ServerResponse.HTTP_BAD_REQUEST = 400;
ServerResponse.HTTP_UNAUTHORIZED = 401;
ServerResponse.HTTP_REQUEST_FAILED = 402;
ServerResponse.HTTP_NOT_FOUND = 404;
ServerResponse.HTTP_METHOD_NOT_ALLOWED = 405;
ServerResponse.HTTP_CONFLICT = 409;
ServerResponse.HTTP_TOO_LARGE = 413;
ServerResponse.HTTP_TOO_MANY_REQUEST = 429;
ServerResponse.HTTP_SERVER_ERROR = 500;
ServerResponse.HTTP_METHOD_NOT_IMPLEMENTED = 501;
ServerResponse.HTTP_CONNECTION_REFUSED = 502;
ServerResponse.HTTP_SERVICE_UNAVAILABLE = 503;