node.js - Access to mountpath variable from inside a template using express -


is there clean, recomended way access mountpath inside template, express app can run both standalone , part of app (by means of app.use), paths pointing correct destination either way?

something like:

{{mountpath}}route/to/file 

so in case app running standalone, mountpath /, , in case running submodule, mountpath /foo/

note: i'm using handlebars.

express.static middleware responsible serving static assets of express application. how works:

  • serve static content app "public" directory in application directory

    // /style.css etc

    app.use(express.static(__dirname + '/public'));

  • mount middleware @ "/static" serve static content when request path prefixed "/static"

    // /static/style.css etc.

    app.use('/static', express.static(__dirname + '/public'));

  • serve static files multiple directories, give precedence "./public" on others

    app.use(express.static(__dirname + '/public'));

    app.use(express.static(__dirname + '/files'));

    app.use(express.static(__dirname + '/uploads'));

in case can use second case i.e.; mounting files on path '/static', whatever files under folder public can accesses src='/static/filename.xyz' create folder name public sub folders css, js, images or if want css mounted on path /css :

app.use('/css',express.static(path.join(__dirname, 'module1/css'))); 

now can use css files in entire application :

 <link href="css/style.css" rel="stylesheet"> 

Popular posts from this blog