Created
April 3, 2014 19:42
-
-
Save dylants/9961432 to your computer and use it in GitHub Desktop.
Generate an href for a resource. Generates a full path to the resource in "development" environment, relative path in other environments ("production").
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Generates an href to a resource depending on the environment. In development, | |
* this will be fully qualified based on the host. Otherwise it will be relative | |
* without any host information. | |
* | |
* @param {Object} app The express app object | |
* @param {Object} req The express request object | |
* @param {String} path The relative path to the resource, for example "/members/123" | |
* @return {String} An href to this resource | |
*/ | |
function generateHref(app, req, path) { | |
var href = ""; | |
// in development mode, include host in href | |
if (app.get("env") === "development") { | |
href = href + url.format({ | |
protocol: req.protocol, | |
host: req.get("host") | |
}); | |
} | |
href = href + path; | |
return href; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment