ok.js
1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* 200 (OK) Response
*
* Usage:
* return res.ok();
* return res.ok(data);
* return res.ok(data, 'auth/login');
*
* @param {Object} data
* @param {String|Object} options
* - pass string to render specified view
*/
module.exports = function sendOK (data, options) {
// Get access to `req`, `res`, & `sails`
var req = this.req;
var res = this.res;
var sails = req._sails;
sails.log.silly('res.ok() :: Sending 200 ("OK") response');
// Set status code
res.status(200);
// If appropriate, serve data as JSON(P)
if (req.wantsJSON) {
return res.jsonx(data);
}
// If second argument is a string, we take that to mean it refers to a view.
// If it was omitted, use an empty object (`{}`)
options = (typeof options === 'string') ? { view: options } : options || {};
// If a view was provided in options, serve it.
// Otherwise try to guess an appropriate view, or if that doesn't
// work, just send JSON.
if (options.view) {
return res.view(options.view, { data: data });
}
// If no second argument provided, try to serve the implied view,
// but fall back to sending JSON(P) if no view can be inferred.
else return res.guessView({ data: data }, function couldNotGuessView () {
return res.jsonx(data);
});
};