Skip to content

app.mountpath is not set correctly when used within the app itself #1975

@thomasfr

Description

@thomasfr

Hi,

I have to use app.mountpath in the views of my app, as a prefix for some urls. In this example i used it for the url to a css file which is in a public directory in the app (/app/public), but it could also be used for dynamically creating links depending on the mountpath.

What i did is basically this:

// /index.js

var app = express()
var mountpoint = "/foo";
app.use(mountpoint, require("./app/index.js"))
// /app/index.js

var app = module.exports = express()

app.locals.baseurl = app.mountpath.replace(/\/$/, "") // <-- of course, this is set to '/' at this moment, because it is not yet mounted under 'mountpoint' (/foo)

var router = express.Router()
var route = router.route("/bar")
route.get(function(request, response, next) {
  response.render("bar")
});

app.use(router)
// /app/views/bar.jade
doctype html
html(lang="en")
  head
    meta(charset="utf-8")
    link(href="#{baseurl}/css/style.css", rel="stylesheet")

When requesting /foo/bar the link href should be /foo/css/style.css but it is /css/style.css

Maybe i am using it in a way it was not intended. If so, please tell me how i can achieve the same result.

Thanks
cheers
Thomas

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions