Skip to content

Instantly share code, notes, and snippets.

@dylants
Created April 3, 2014 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dylants/9961432 to your computer and use it in GitHub Desktop.
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").
/**
* 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