Server Response
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
createResponseObject
Create a data response object.
createResponseObject(httpCode, resultPayload)
sendResponse
Send a server response to caller
sendResponse(code, res, data)
sendOk
Send an HTTP_OK server response to caller
sendOk(res, data)
sendCreated
Send an HTTP_CREATED server response to caller
sendCreated(res, data)
sendMovedPermanently
Send an HTTP_MOVED_PERMANENTLY server response to caller
sendMovedPermanently(res, data)
sendNotFound
Send an HTTP_NOT_FOUND server response to caller
sendNotFound(res, data)
sendInvalidRequest
Send an HTTP_BAD_REQUEST server response to caller
sendInvalidRequest(res, data)
sendInvalidSession
Send an HTTP_BAD_REQUEST server response to caller
sendInvalidSession(res, data)
sendInvalidUserCredentials
Send an HTTP_UNAUTHORIZED server response to caller
sendInvalidUserCredentials(res, data)
sendRequestFailed
Send an HTTP_REQUEST_FAILED server response to caller
sendRequestFailed(res, data)
sendDataConflict
Send an HTTP_CONFLICT server response to caller.
sendDataConflict(res, data)
sendTooLarge
Send an HTTP_TOO_LARGE server response to caller
sendDataConflict(res, data)
sendTooManyRequests
Send an HTTP_TOO_MANY_REQUEST server response to caller
sendTooManyRequests(res, data)
sendServerError
Send an HTTP_SERVER_ERROR server response to caller
sendServerError(res, data)
sendInternalError
Alias for sendResponseServerError
sendInternalError(res, data)
sendMethodNotImplemented
Send an HTTP_METHOD_NOT_IMPLEMENTED server response to caller
sendMethodNotImplemented(res, data)
sendConnectionRefused
Send an HTTP_CONNECTION_REFUSED server response to caller
sendUnavailableError(res, data)
sendUnavailableError
Send an HTTP_METHOD_NOT_IMPLEMENTED server response to caller
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;