I would like to know if there is a way to returns a specific HTTP status code from within a remote method.
I can see that there is a callback function which we can pass an error object, but how do we define the HTTP status code?
I would like to know if there is a way to returns a specific HTTP status code from within a remote method.
I can see that there is a callback function which we can pass an error object, but how do we define the HTTP status code?
If you wish to use an HTTP status code to notify of an error, you can pass an error in the remote methods callback method:
var error = new Error("New password and confirmation do not match");
error.status = 400;
return cb(error);
You can find more information about the error object here: Error object
If you wish to just alter the HTTP response status without using an error, you can use one of the two methods defined by either #danielrvt or #superkhau. To obtain the reference to the request object mentioned by #superkhau, in your method registration you can define an additional argument that will be passed to your remote method. See HTTP mapping of input arguments
{ status: 404, message: 'Not found'}
is there a better approach? –
Stortz Lets assume that you have a CoffeShop Model and you want to send status 404 if the item is not in your db.
CoffeeShop.getName = function(req, res, cb) {
var shopId = req.query.id;
CoffeeShop.findById(shopId, function(err, instance) {
if (instance && instance.name){
var response = instance.name;
cb(null, response);
}
else {
res.status(404);
cb(null);
}
});
}
CoffeeShop.remoteMethod(
'getName',
{
http: { path: '/getname', verb: 'get' },
accepts: [{arg: 'req', type: 'object', http: { source: 'req' }},
{ arg: 'res', type: 'object', http: { source: 'res' }}],
returns: { arg: 'name', type: 'string' }
}
);
When using an async
remote method function, you need to let the async
function throw any encountered errors, rather than trying to catch them and call return
. By calling return
you're telling LoopBack that it should respond as if it were successful.
Here's an example working structure.
AdminDashboard.login = async(body) => {
let username = body.username
let password = body.password
await isDomainAdmin(username, password)
}
AdminDashboard.remoteMethod(
'login',
{
http: {path: '/login', verb: 'put'},
consumes: ['application/json'],
produces: ['application/json'],
accepts: [
{arg: 'body', type: 'Credentials', http: {source: 'body'}}
]
}
)
Just make sure that any internal functions you call like isDomainAdmin
are also either throwing their errors directly, or that you catch them and convert them to an error object like this:
{
statusCode: 401,
message: 'Unauthorized'
}
Where err.statusCode
is the HTTP status code you want LoopBack to return.
You can return any status code just like you would in ExpressJS.
...
res.status(400).send('Bad Request');
...
ctx.req
. There are also major discussions around getCurrentContext: github.com/strongloop/loopback/issues/1676. –
Cruise In your remote method registration:
YourModel.remoteMethod('yourMethod', {
accepts: [
{arg: 'res', type: 'object', http:{source: 'res'}}
],
...
returns: {root: true, type: 'string'},
http: {path: '/:id/data', verb: 'get'}
});
If you just need to modify response status, just do:
ctx.res.status(400);
return cb(null);
In Loopback 4 there is an HttpErrors
class for this. Here are just a few:
// Throw a 400 error
throw new HttpErrors.BadRequest("Invalid request");
// Throw a 403 error
throw new HttpErrors.Forbidden("You don't have permission to do this");
// Throw a 404 error
throw new HttpErrors.NotFound("The data was not found")
You can also inject the Response
object in the controller's constructor and explicitly call it:
// As a constructor argument
@inject(RestBindings.Http.RESPONSE) private res: Response
// In your method
this.res.status(400).send("Invalid request");
© 2022 - 2024 — McMap. All rights reserved.