简体   繁体   English

Javascript文件未加载到Jade文件中

[英]Javascript file not loading into Jade file

Here's the file structure i am using 这是我正在使用的文件结构

-----+root
----------+app
--------------+common
--------------+config
--------------+controllers
--------------------------+rootPage.js
----------+public
--------------+rootPage.jade
----------+server.js

Here's my jade file 这是我的玉档案

doctype
html(lang = 'en')
    head
        title PlanUrNight
        meta(charset = 'utf-8')
        link(rel = 'stylesheet' href = '//maxcdn.bootstrapcdn.com/bootswatch/3.3.0/flatly/bootstrap.min.css')
        link(src='//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js' rel = 'stylesheet')
        link(rel = 'stylesheet' href = './css/rootPage.css')
    body
        nav.navbar.navbar-inverse(role= 'navigation')
            .navbar-header
                button.navbar-toggle.collapsed( type='button', data-toggle='collapse', data-target='#navbar-inverse', aria-expanded='false', aria-controls='navbar')
                    span.sr-only Toggle navigation
                    span.icon-bar
                    span.icon-bar
                    span.icon-bar
                a.navbar-brand(href='#') PlanUrNight
            .collapse.navbar-collapse#navbar-inverse
                ul.nav.navbar-nav
                    li: a( href="#") Home
                .collapse.navbar-collapse.navbar-right
                    .facebook-login-wrapper
                        a.btn.btn-primary(href='/auth/facebook') Facebook
                            span.fa.fa-facebook 
        .container-fluid
            .row
                .col-md-8.col-md-offset-2.main-container
                    .images-container
                        img.drink(src='img/drinking.png')
                        img.dance(src='img/couple_dancing.png')
                        img.club(src='img/club_ball.png')
            .row
                .col-md-2.col-md-offset-6.search-container
                    span.glyphicon.glyphicon-search
            .input-group
                    input.form-control(type='text', placeholder='Search')

    script( src='//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js' type='text/javascript')
    script( src='./controllers/rootPage.js' type='text/javascript')

I have tried multiple variations of the source, but it just doesn't seem to be loading the JavaScript file. 我已经尝试了源代码的多种变体,但似乎并未加载JavaScript文件。 Each time I get an error log in my console, saying Error 404: rootPage.js not found 每次我在控制台中收到错误日志时,都说错误404:找不到rootPage.js

I am using express with node, and in my server.js file I have the following line for serving static files 我正在使用带有节点的express,并且在我的server.js文件中,以下行用于提供静态文件

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

So what am I doing wrong here? 那我在做什么错呢? Does the usage of the app.use line above change the root of my directory in some way so that I need to change the file path to access my JS files? 上面的app.use行的用法是否会以某种方式更改目录的根目录,以便我需要更改文件路径才能访问我的JS文件? Or is there a different way to load JS files in Jade? 还是有其他方法可以在Jade中加载JS文件?

Your file organization is a bit wonky. 您的文件组织有点笨拙。 Based on the one middleware you showed us, all files in the /public folder will be served as-is, but no static files elsewhere will be. 根据您显示给我们的一种中间件,/ public文件夹中的所有文件将按原样提供,但其他位置将没有静态文件。

Generally, jade files that you're rendering with server logic are in a /views folder which is not served directly, but instead available to server side route handlers or controller logic to call res.render with. 通常,使用服务器逻辑渲染的Jade文件位于/ views文件夹中,该文件夹不会直接提供,而是可用于服务器端路由处理程序或控制器逻辑来调用res.render。

So if you have clientside JS files you want to serve as static content you need them either under the /public folder or create more static middleware calls to point to whatever folder they are in. 因此,如果您有要用作静态内容的客户端JS文件,则需要它们在/ public文件夹下,或者需要创建更多静态中间件调用以指向它们所在的任何文件夹。

/** Edit after first two comments **/ / **在前两个评论后编辑** /

Sorry for not providing more examples, etc before, I was on my phone. 抱歉,我以前在手机上没有提供更多示例,等等。

Wonky is perhaps a harsh term and I'm sorry. Wonky可能是一个苛刻的用语,对不起。 What I meant was it doesn't really match the standard layouts I've seen. 我的意思是,它实际上与我见过的标准布局不匹配。 There's a few ways to do it, but most small(ish) Express projects at least start out with the template generated by the express command line tool. 有几种方法可以执行此操作,但是大多数小型(ish)Express项目至少要从Express命令行工具生成的模板开始。

In that case, all the stuff in your ./root/app directory would be server-side code that doesn't get directly served to the client ever. 在这种情况下,。/ ./root/app目录中的所有内容都是服务器端代码,永远不会直接提供给客户端。 Most of the sites I've seen (exception being the default template from the MEAN.js project) follow a pattern something like this: 我见过的大多数网站(例外是MEAN.js项目的默认模板)都遵循以下模式:

app - errors - models - controllers - routes - views public - css - js - img package.json server.js 应用-错误-模型-控制器-路线-公开视图-CSS-js-img package.json server.js

Sometimes there's a lib folder that's a peer of app where you put utility stuff. 有时会有一个lib文件夹,它是应用程序的对等物,您可以在其中放置实用程序内容。 'views' is where all the jade templates live. 所有翡翠模板都位于“视图”中。

Everything in the public folder is exposed via a single middleware like you did: 公用文件夹中的所有内容都通过单个中间件公开,就像您所做的那样:

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

Everything else will not get served as static files. 其他所有内容都不会作为静态文件提供。 If you have a clientside JS structure that uses an MVC pattern, you'd then have model, view, and controller folders under ./public/js 如果您具有使用MVC模式的客户端JS结构,则可以在./public/js下拥有model,view和controller文件夹。

The MEAN.js folks take a different approach, making each logical component of the app (eg user management, etc) into a module and then organizing each module as folders that look like ./<module name>/server and ./<module name>/client with structure for models, controllers, etc, under each of those depending on if it's server code or client code. MEAN.js人员采用不同的方法,将应用程序的每个逻辑组件(例如,用户管理等)放入一个模块中,然后将每个模块组织为类似于./<module name>/server./<module name>/client文件夹./<module name>/client ,在每个模型下都有模型,控制器等的结构,具体取决于服务器代码还是客户端代码。

You're correct on how to add more more static middleware. 您如何添加更多静态中间件是正确的。

尝试

  script( src='./app/controllers/rootPage.js' type='text/javascript')

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

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