This method is used to send a 400 ("Bad Request") response back down to the client indicating that the request is invalid. This usually means it contained invalid parameters or headers, or tried to do something impossible based on your app logic.
return res.badRequest();
Or:
return res.badRequest(data);
return res.badRequest(data, pathToView);
Like the other built-in custom response modules, the behavior of this method is customizable.
By default, it works as follows:
data
as JSON. If no data
is provided a default response body will be sent (the string "Bad Request"
).pathToView
was provided, Sails will attempt to use that view.pathToView
was not provided, Sails will try to guess an appropriate view (see res.view()
for details). If Sails cannot guess a workable view, it will just send JSON.data
argument will be accessible as a view local: data
.Using the default view:
if ( req.param('amount') < 500 )
return res.badRequest(
'Transaction limit exceeded. Please try again with an amount less than $500.'
);
}
With a custom view:
if ( req.param('amount') < 500 )
return res.badRequest(
'Transaction limit exceeded. Please try again with an amount less than $500.',
'salesforce/leads/edit'
);
}
- This method is terminal, meaning it is generally the last line of code your app should run for a given request (hence the advisory usage of
return
throughout these docs).res.badRequest()
(like other userland response methods) can be overridden or modified. It runs the response method defined in/responses/badRequest.js
, which is bundled automatically in newly generated Sails apps. If abadRequest.js
response method does not exist in your app, Sails will implicitly use the default behavior.- This method is called automatically if a call to
req.validate()
fails any of its validation checks.- By default, the specified error (
err
) will be excluded if the app is running in the "production" environment (i.e.process.env.NODE_ENV === 'production'
).