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
name
string Name of the header to setvalue
string | string[] Value of the header to set
Returns this
respond.withHeader('Accept', 'text/json')
Sets the body for the response object
Parameters
body
any 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
status
number The status code to set
Returns this
respond.withStatus(404)
Sets the URL of the response object
Parameters
url
string The response object's URL
Returns this
respond.withUrl('/api')
Shortcut to set the status, body, and headers in one call
Parameters
status
number Status code of the responsebody
optional any Body of the responseheaders
optional 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
body
optional any Body of the responseheaders
optional 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
body
optional any Body of the responseheaders
optional 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');