forked from posm/OpenMapKitServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurl.js
More file actions
44 lines (40 loc) · 1.36 KB
/
url.js
File metadata and controls
44 lines (40 loc) · 1.36 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
var settings = require('../settings');
var Url = module.exports = {};
/**
* Returns a fully qualified url for a resource in the
* data directory.
*
* @param req
* @param path
* @param fileName (optional)
*/
Url.dataDirFileUrl = function (req, path, fileName) {
path = encodeURIComponent(path).replace(/%2F/g, '/'); // keep slashes
fileName = encodeURIComponent(fileName).replace(/%2F/g, '/'); // keep slashes
var url = base = req.protocol + '://' + req.headers.host + '/omk/data';
path[0] === '/' ? url += path : url += '/' + path;
if (typeof fileName === 'undefined' || fileName === null) return url;
path[path.length -1] === '/' ? url += fileName : url += '/' + fileName;
return url;
};
/**
* Returns a fully qualified URL for an API endpoint.
*
* @param req - http request that is pending
* @param path - the API path from he base of the URL
* @returns {string} - the full URL to the endpoint
*/
Url.apiUrl = function (req, path) {
path = encodeURIComponent(path).replace(/%2F/g, '/'); // keep slashes
var base = req.protocol + '://' + req.headers.host;
return path[0] === '/' ? base + path : base + '/' + path;
};
/**
* Just makes sure a string ends with a /
*
* @param str - any string
* @returns {string} - string with / ending
*/
Url.endWithSlash = function (str) {
return str[str.length -1] === '/' ? str : str + '/';
};