Link to static files from Jade template rendered from a sub route

后端 未结 1 1819
一生所求
一生所求 2021-01-22 18:57

I have a very basic problem with Node.js/Express/Jade that\'s surprisingly hard to describe. In my node.js application I use the Express framework for routing of HTTP requests.

相关标签:
1条回答
  • 2021-01-22 19:50

    I am guessing that your Jade template has something that looks like the following:

    doctype html
      head
        link(rel="stylesheet", type="text/css", href="css/style.css")
        ...
    

    The href for the stylesheet is a relative path, meaning your browser will look for the CSS file relative to the page it is currently on. For example:

    • http://example.com/abouthttp://example.com/css/style.css
    • http://example.com/about/companyhttp://example.com/about/css/style.css

    You can change the href to an absolute path so the CSS file location will always be constant no matter what sub-directory you are in:

    link(rel="stylesheet", type="text/css", href="/css/style.css")
    

    The key change here is the forward slash at the beginning of the href, turning the relative path into an absolute one.

    0 讨论(0)
提交回复
热议问题