This repository has been archived by the owner on May 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
53 lines (46 loc) · 1.48 KB
/
utils.js
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
49
50
51
52
53
var util = require("util"),
log = util.log,
fs = require("fs");
utils = {
/**
* Serves a file to the response
* @method serveStaticFile
* @param {string} filename Full path to get the file
* @param {Object} req Request
* @param {Object} res Response
*/
serveStaticFile: function (filename, req, res, statusCode) {
statusCode = statusCode || 200;
log("[Mockaccino] Getting file " + filename);
fs.readFile(__dirname + filename, function(err, data){
if (err) {
res.send(statusCode, data.toString());
} else {
res.send(404, "Couldn't find the file to be served.... grrrrrr " + __dirname + filename);
}
});
},
/**
* Returns the property identified by the path (in dot notation)
* @method walkJSONDot
* @param {object} obj An object
* @param {string} path A path expressed in dot notation
* @return {object} The part of object referenced by the path
*/
walkJSONDot: function (obj, path) {
var current = obj;
if ((typeof obj !== "object") || (!obj)) {
return;
}
path.split(".").every(function (property) {
current = current[property];
if (current === undefined) {
return false;
} else {
return true;
}
});
return current;
}
};
module.exports = utils;