You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
varexpress=require('express');varBluebird=require('bluebird');varResponse=require('node-friendly-response');varResponseError=Response.ResponseError;varapp=express();app.get('/',function(req,res){Bluebird.resolve(req.body).tap(function(body){if(!body.value){thrownewResponseError.BadRequest('Property "value" is mandatory');}}).get('value').tap(function(value){if(value!==42){thrownewResponseError.PaymentRequired('$$$$');}}).then(__doSomething).then(res.ok.bind(res)).catch(ResponseError,res.sendError.bind(res));});
Available Status Codes
res.continue// send a response with status 100res.switchingProtocols// send a response with status 101res.processing// send a response with status 102res.ok// send a response with status 200res.created// send a response with status 201res.accepted// send a response with status 202res.nonAuthoritativeInformation// send a response with status 203res.noContent// send a response with status 204res.resetContent// send a response with status 205res.partialContent// send a response with status 206res.multiStatus// send a response with status 207res.multipleChoices// send a response with status 300res.movedPermanently// send a response with status 301res.movedTemporarily// send a response with status 302res.seeOther// send a response with status 303res.notModified// send a response with status 304res.useProxy// send a response with status 305res.temporaryRedirect// send a response with status 307res.badRequest// send a response with status 400res.unauthorized// send a response with status 401res.paymentRequired// send a response with status 402res.forbidden// send a response with status 403res.notFound// send a response with status 404res.methodNotAllowed// send a response with status 405res.notAcceptable// send a response with status 406res.proxyAuthenticationRequired// send a response with status 407res.requestTimeout// send a response with status 408res.conflict// send a response with status 409res.gone// send a response with status 410res.lengthRequired// send a response with status 411res.preconditionFailed// send a response with status 412res.requestTooLong// send a response with status 413res.requestUriTooLong// send a response with status 414res.unsupportedMediaType// send a response with status 415res.requestedRangeNotSatisfiable// send a response with status 416res.expectationFailed// send a response with status 417res.insufficientSpaceOnResource// send a response with status 419res.methodFailure// send a response with status 420res.unprocessableEntity// send a response with status 422res.locked// send a response with status 423res.failedDependency// send a response with status 424res.internalServerError// send a response with status 500res.notImplemented// send a response with status 501res.badGateway// send a response with status 502res.serviceUnavailable// send a response with status 503res.gatewayTimeout// send a response with status 504res.httpVersionNotSupported// send a response with status 505res.insufficientStorage// send a response with status 507
res.isInfo()// For status codes 1xx;res.isSuccess()// For status codes 2xx;res.isRedirect()// For status codes 3xx;res.isClientError()// For status codes 4xx;res.isServerError()// For status codes 5xx;res.isError()// For status codes 4xx and 5xx;
and also...
res.isContinue()// if status code is 100res.isSwitchingProtocols()// if status code is 101res.isProcessing()// if status code is 102res.isOk()// if status code is 200res.isCreated()// if status code is 201res.isAccepted()// if status code is 202res.isNonAuthoritativeInformation()// if status code is 203res.isNoContent()// if status code is 204res.isResetContent()// if status code is 205res.isPartialContent()// if status code is 206res.isMultiStatus()// if status code is 207res.isMultipleChoices()// if status code is 300res.isMovedPermanently()// if status code is 301res.isMovedTemporarily()// if status code is 302res.isSeeOther()// if status code is 303res.isNotModified()// if status code is 304res.isUseProxy()// if status code is 305res.isTemporaryRedirect()// if status code is 307res.isBadRequest()// if status code is 400res.isUnauthorized()// if status code is 401res.isPaymentRequired()// if status code is 402res.isForbidden()// if status code is 403res.isNotFound()// if status code is 404res.isMethodNotAllowed()// if status code is 405res.isNotAcceptable()// if status code is 406res.isProxyAuthenticationRequired()// if status code is 407res.isRequestTimeout()// if status code is 408res.isConflict()// if status code is 409res.isGone()// if status code is 410res.isLengthRequired()// if status code is 411res.isPreconditionFailed()// if status code is 412res.isRequestTooLong()// if status code is 413res.isRequestUriTooLong()// if status code is 414res.isUnsupportedMediaType()// if status code is 415res.isRequestedRangeNotSatisfiable()// if status code is 416res.isExpectationFailed()// if status code is 417res.isInsufficientSpaceOnResource()// if status code is 419res.isMethodFailure()// if status code is 420res.isUnprocessableEntity()// if status code is 422res.isLocked()// if status code is 423res.isFailedDependency()// if status code is 424res.isInternalServerError()// if status code is 500res.isNotImplemented()// if status code is 501res.isBadGateway()// if status code is 502res.isServiceUnavailable()// if status code is 503res.isGatewayTimeout()// if status code is 504res.isHttpVersionNotSupported()// if status code is 505res.isInsufficientStorage()// if status code is 507
and also... listeners for events...
varServerResponse=require('node-friendly-response').ServerResponse;ServerResponse.onContinue(callback)// if status code is 100ServerResponse.onSwitchingProtocols(callback)// if status code is 101ServerResponse.onProcessing(callback)// if status code is 102ServerResponse.onOk(callback)// if status code is 200ServerResponse.onCreated(callback)// if status code is 201ServerResponse.onAccepted(callback)// if status code is 202ServerResponse.onNonAuthoritativeInformation(callback)// if status code is 203ServerResponse.onNoContent(callback)// if status code is 204ServerResponse.onResetContent(callback)// if status code is 205ServerResponse.onPartialContent(callback)// if status code is 206ServerResponse.onMultiStatus(callback)// if status code is 207ServerResponse.onMultipleChoices(callback)// if status code is 300ServerResponse.onMovedPermanently(callback)// if status code is 301ServerResponse.onMovedTemporarily(callback)// if status code is 302ServerResponse.onSeeOther(callback)// if status code is 303ServerResponse.onNotModified(callback)// if status code is 304ServerResponse.onUseProxy(callback)// if status code is 305ServerResponse.onTemporaryRedirect(callback)// if status code is 307ServerResponse.onBadRequest(callback)// if status code is 400ServerResponse.onUnauthorized(callback)// if status code is 401ServerResponse.onPaymentRequired(callback)// if status code is 402ServerResponse.onForbidden(callback)// if status code is 403ServerResponse.onNotFound(callback)// if status code is 404ServerResponse.onMethodNotAllowed(callback)// if status code is 405ServerResponse.onNotAcceptable(callback)// if status code is 406ServerResponse.onProxyAuthenticationRequired(callback)// if status code is 407ServerResponse.onRequestTimeout(callback)// if status code is 408ServerResponse.onConflict(callback)// if status code is 409ServerResponse.onGone(callback)// if status code is 410ServerResponse.onLengthRequired(callback)// if status code is 411ServerResponse.onPreconditionFailed(callback)// if status code is 412ServerResponse.onRequestTooLong(callback)// if status code is 413ServerResponse.onRequestUriTooLong(callback)// if status code is 414ServerResponse.onUnsupportedMediaType(callback)// if status code is 415ServerResponse.onRequestedRangeNotSatisfiable(callback)// if status code is 416ServerResponse.onExpectationFailed(callback)// if status code is 417ServerResponse.onInsufficientSpaceOnResource(callback)// if status code is 419ServerResponse.onMethodFailure(callback)// if status code is 420ServerResponse.onUnprocessableEntity(callback)// if status code is 422ServerResponse.onLocked(callback)// if status code is 423ServerResponse.onFailedDependency(callback)// if status code is 424ServerResponse.onInternalServerError(callback)// if status code is 500ServerResponse.onNotImplemented(callback)// if status code is 501ServerResponse.onBadGateway(callback)// if status code is 502ServerResponse.onServiceUnavailable(callback)// if status code is 503ServerResponse.onGatewayTimeout(callback)// if status code is 504ServerResponse.onHttpVersionNotSupported(callback)// if status code is 505ServerResponse.onInsufficientStorage(callback)// if status code is 507
and also... listeners for error events...
ServerResponse.onResponseError(callback);// if status code greather than 400ServerResponse.removeAllListeners(events);// remove all listeners for an event, the events is an array of status codes or the string 'response_error'
License
The MIT License (MIT)
Copyright (c) 2015 Federico Scarpa <[email protected]>.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.