简体   繁体   English

Node.js路由错误:给出'X-Content-Type:nosniff'时,不允许脚本MIME类型

[英]Node.js route error: script MIME types are not allowed when 'X-Content-Type: nosniff' is given

My css and js files are not found / does not load under second route . 我的cssjs文件未找到/不在第二条路径下加载。 They do load fine under first. 他们确实在第一下很好。

Why does this happen ? 为什么会这样?

  // **********************  INDEX PAGE  *******************************************
    app.get('/LimeLINE', (req, res) => {
        let sTopHtml = fs.readFileSync(__dirname + '/components/top.html', 'utf8')
        let sMainHtml = fs.readFileSync(__dirname + '/html/index.html', 'utf8')
        let sBottomHtml = fs.readFileSync(__dirname + '/components/bottom.html', 'utf8')

        // replace placeholders
        sTopHtml = sTopHtml.replace('{{title}}', 'LimeLINE: Get Started')

        res.send(sTopHtml + sMainHtml + sBottomHtml)
    })

    app.get('/LimeLINE/activate/:token', (req, res) => {
        let sToken = req.params.token;
        if (token === sToken) {
            user.activateUser(res, sToken)
        }
        let sTopHtml = fs.readFileSync(__dirname + '/components/top.html', 'utf8')
        let sMainHtml = fs.readFileSync(__dirname + '/html/index.html', 'utf8')
        let sBottomHtml = fs.readFileSync(__dirname + '/components/bottom.html', 'utf8')
        // replace placeholders
        sTopHtml = sTopHtml.replace('{{title}}', 'LimeLINE: Welcome')
        sMainHtml = sMainHtml.replace('{{click-trigger}}', 'click-trigger')

        res.send(sTopHtml + sMainHtml + sBottomHtml)
    })

You should avoid using relative urls in templates which will be served from different routes. 您应避免在模板中使用相对的网址,这些网址将通过不同的路径进行投放。 In this case same html is getting served from http://<hostname>/LimeLINE as well as http://<hostname>/LimeLINE/activate/:token . 在这种情况下,将从http://<hostname>/LimeLINE以及http://<hostname>/LimeLINE/activate/:token提供相同的html。 The relative url of ../css/main.css will point to different locations in those two cases. 在这两种情况下, ../css/main.css的相对URL将指向不同的位置。 Since the browser doesn't know about the directory structure on your server, it simply constructs the path relative to current url (in address bar). 由于浏览器不知道服务器上的目录结构,因此它只是构造相对于当前url的路径(在地址栏中)。

For the first case it becomes http://hostname/../css/main.css which is same as http://hostname/css/main.css ; 对于第一种情况,它变为http://hostname/../css/main.css ,与http://hostname/css/main.css相同;

And in second case it is http://<hostname>/LimeLINE/activate/../css/main.css which is same as http://<hostname>/LimeLINE/css/main.css . 第二种情况是http://<hostname>/LimeLINE/activate/../css/main.csshttp://<hostname>/LimeLINE/css/main.css

Just change to absolute urls (or relative to host urls, starting with /) and your code will work fine. 只需更改为绝对URL(或相对于主机URL,以/开头),您的代码即可正常工作。 Instead of href="../css/main.css" use href="/css/main.css" 代替href="../css/main.css"使用href="/css/main.css"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何解决错误“未在脚本名称加载脚本”,因为在给出“X-Content-Type:nosniff”时不允许使用非脚本MIME类型 - How to address error "Did not load script at 'script name' because non script MIME types are not allowed when 'X-Content-Type: nosniff' is given 拒绝将 *path_to_bundle* 作为脚本执行,因为给出了“X-Content-Type: nosniff”,并且它的 Content-Type 不是脚本 MIME 类型 - Refused to execute *path_to_bundle* as script because “X-Content-Type: nosniff” was given and its Content-Type is not a script MIME type NGINX:X-Content-Type-Options nosniff导致错误的资源/ MIME类型 - NGINX: X-Content-Type-Options nosniff results in wrong resource/MIME-types 错误:由于 MIME 类型(“text/html”)不匹配而被阻止(X-Content-Type-Options: nosniff) - Error: Blocked due to MIME type (“text/html”) mismatch (X-Content-Type-Options: nosniff) 来自 Firefox 的错误:MIME 类型(“text/plain”)不匹配(X-Content-Type-Options:nosniff) - Error from Firefox: MIME type (“text/plain”) mismatch (X-Content-Type-Options: nosniff) 由于 MIME 类型 (“text/html”) 不匹配 (X-Content-Type-Options: nosniff),来自“http://localhost:3000/script.js”的资源被阻止 - The resource from “http://localhost:3000/script.js” was blocked due to MIME type (“text/html”) mismatch (X-Content-Type-Options: nosniff) 由于MIME类型不匹配,功能被阻止(X-Content-Type-Options:nosniff) - Function was blocked due to MIME type mismatch (X-Content-Type-Options: nosniff) 由于 MIME 类型不匹配而被阻止的资源(X-Content-Type-Options: nosniff) - Resource blocked due to MIME type mismatch (X-Content-Type-Options: nosniff) VANILLA.JS客户端路由器 + Webpack-&gt;&gt; ressource blocked(由于Mime Type(“ text/html”)不匹配(x-content-type-type-type-options:nosniff:nosniff)) - Vanilla.js Client Side Router + Webpack --> Ressource blocked (due to MIME type (“text/html”) mismatch (X-Content-Type-Options: nosniff)) 由于 MIME 类型 (“text/html”) 不匹配 (X-Content-Type-Options: &gt; nosniff),资源被阻止 - The resource was blocked due to MIME type (“text/html”) mismatch (X-Content-Type-Options: > nosniff)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM