繁体   English   中英

Node.JS / Express应用程序中的Jade模板找不到外部Javascript文件?

[英]Jade template in Node.JS/Express app can't find external Javascript file?

我有一个使用Jade模板引擎的Node.JS / Express应用程序。 我有一个JavaScript文件,该文件是目录中的正上方我的看法目录包含我的玉意见。 给定下面的代码,我认为该视图应该能够找到引用的服务器端Javascript文件,名为 common_routines ,但是当请求该视图时,我收到一个404错误,提示找不到外部Javascript文件。 这使我感到困惑,因为我认为Jade模板执行服务器端,那么为什么这会触发Jade render()调用中的404 error

这是翡翠视图:

block content
    script(src="../common_routines.js")
    div(style="padding:5px")
        h4 #{product.name}
    if (product.brand != null)
        p Brand: #{product.brand}
    div(class="img_and_desc")
        img(src=product.imageUrl, class="product_image", align="left")
        if (product.minimumPrice == product.maximumPrice)
            p Price: #[strong #{product.minimumPrice}]
        else
            p Price: #[strong #{product.minimumPrice}] to #[strong #{product.maximumPrice}]
        if (product.cashBack > 0)
            p Cash Back amount: #[strong #{product.cashBack}]
        if (product.freeShipping)
            p(class="text-emphasized-1") *Free Shipping!
        if (product.onSale)
            p(class="text-emphasized-2") Discounted Item

这是渲染视图的路由文件的摘录,其中触发了if (err)分支:

           res.render(
                'ajax-product-details',
                locals,
                function (err, html) {
                    if (err)
                    // A rendering error occurred.
                        throw new Error("Rendering error(type:" + err.type + ", message: " + err.message);
                    else {
                        // Success. Send back the rendered HTML.
                        res.send(html);
                        return;
                    }
                });

这是我在WebStorm调试器控制台窗口窗格中看到的错误。 请注意,尽管在Jade文件引用中可以清楚地看到前面的“ ..”相对目录引用,但文件显示为/common_routines.js, 而没有前面的“ ..”相对目录引用("script(src="../common_routines.js")):

[Express][127.0.0.1][GET][DESKTOP] 2016-07-20T10:07:34.940Z /common_routines.js?_=1469009245957
(development error handler) Status code(404) message: Not Found

这是common_routines.js的内容:

function truncateString(str, maxLen)
{
    if (str.length > maxLen)
        return str.substring(0, maxLen - 1) + '&hellip';
    return str;
}

module.exports =
{
    truncateString: truncateString
}

就像我说的那样, common_routines.js位于Jade视图上方的目录中,因此出于某种原因,此外部文件引用无效吗?:

script(src="../common_routines.js")

您只能在前端引用public文件夹中的静态文件(如果已将静态资产设置为从该文件夹提供)。 您可以将common_routines.js文件放在public内部的文件夹中,例如public/js/ 现在,您可以使用script(src="/js/common_routines.js")在您的视图文件中引用它

如果您想在jade文件中而不是在前端js文件中使用这些功能,则需要使用var commonRoutines = require('../common_routines.js');将该功能作为一个包包含在内var commonRoutines = require('../common_routines.js'); 在服务器端js文件中。 现在,您可以将此对象变量与上下文对象一起传递到视图文件。 如前所述,在渲染时将locals对象传递给视图文件,您可以像这样进行操作:

var common_routines = require('../common_routines.js');
var locals = { common_routines: common_routines };
res.render( 'ajax-product-details', locals);

现在,您可以在ajax-product-details文件中使用common_routines.js中的函数。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM