Respond is used to create responses with a fluent API
import RESPOND_PROVIDERS, {Respond} from 'respond-ng';
const injector = Injector.resolveAndCreate([
RESPOND_PROVIDERS
]);
const respond = injector.get(Respond);
respond.withBody({ error: 'Not Found' }).withStatus(404);Adds a header to the response object
Parameters
namestring Name of the header to setvaluestring | string[] Value of the header to set
Returns this
respond.withHeader('Accept', 'text/json')Sets the body for the response object
Parameters
bodyany Body string or object. If it is an object it will be serialized to JSON
Returns this
respond.withBody({ auth: true })Sets the status code of the response object. If the code is less than 200 or greater than 299 then the response will be treated like a failure.
Parameters
statusnumber The status code to set
Returns this
respond.withStatus(404)Sets the URL of the response object
Parameters
urlstring The response object's URL
Returns this
respond.withUrl('/api')Shortcut to set the status, body, and headers in one call
Parameters
statusnumber Status code of the responsebodyoptional any Body of the responseheadersoptional Headers Headers object to use
Returns this
const headers = new Headers();
headers.set('Accept', 'text/json');
respond.with(500, { error: 'Internal Server Error' }, headers)Shortcut to set the body and headers with a 200 status code
Parameters
bodyoptional any Body of the responseheadersoptional Headers Headers object to use
Returns this
const headers = new Headers();
headers.set('Accept', 'text/json');
respond.ok({ authenticated: true }, headers)Shortcut to set the body and headers with a 500 status code
Parameters
bodyoptional any Body of the responseheadersoptional Headers Headers object to use
Returns this
const headers = new Headers();
headers.set('Accept', 'text/json');
respond.error({ authenticated: false }, headers)Serializes the response, then creates and registers a new RequestMatcher for the response
Returns RequestMatcher
respond.ok().when.get('/api');