Sends a 404 ("Not Found") response using either res.json() or res.view(). Called automatically when Sails receives a request which doesn't match any of its explicit routes or route blueprints (i.e. serves the 404 page).
When called manually from your app code, this method is normally used to indicate that the user-agent tried to find, update, or delete something that doesn't exist.
return res.notFound();
Or:
return res.notFound(data);
return res.notFound(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 "Not Found"
).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:
return res.notFound();
With a custom view:
Pet.findOne()
.where(name: 'fido')
.exec(function(err, fido) {
if (err) return res.serverError(err);
if (!fido) return res.notFound(undefined,'pet/sorry-that-pet-has-moved');
// ...
})
- 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.notFound()
(like other userland response methods) can be overridden or modified. It runs the response method defined in/responses/notFound.js
, which is bundled automatically in newly generated Sails apps. If anotFound.js
response method does not exist in your app, Sails will implicitly use the default behavior.- If
pathToView
refers to a missing view, this method will respond as if the request "wants JSON". +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'
).