使用Node和express时找不到Jquery

时间:2015-10-08 16:13:31

标签: jquery node.js express

我在堆栈上看了几个类似的问题(比如this)但是它们似乎都没有帮助我,即使我的问题看起来很相似。

我是节点和服务器端脚本的新手,并且不希望使用更多的框架/组件。

我有一个完全用javascript(jquery)编写的绘图应用程序,我想使用node作为我的后端来托管它。

我的文件夹结构如下:

├── README.md
├── app.js
├── css
├── db.js
├── model.js
├── package.json
├── public
│   ├── css
│   │   └── simple.css
│   └── js
│       ├── jquery.min.js
│       └── testjs.js
├── route.js
├── schema.sql
├── schema_macaronic.sql
├── node_modules +
└── views
    ├── 404.jade
    ├── index.jade
    ├── signin.jade
    └── signup.jade

我的所有代码都在public/js/testjs.js。 testjs.js使用jquery,下划线和其他一些js库。我想在我的观看index.jadesignin.jade

中调用此文件中的函数

我的app.js

中有以下一行
app.use(express.static(path.join(__dirname, 'public')));    
app.use('/jquery', express.static(path.join(__dirname, '/public/js')));

并在signin.jade我看起来像这样:

doctype html
html(lang='en')
    head
        link(rel='stylesheet', href='/css/simple.css')
        script(type="type/javascript",src="/jquery/jquery.min.js")
        //script(type="type/javascript",src="js/testjs.js")
        title #{title}
    body
        script.
            $(document).ready(function() {
                console.log('hello form js and jquery')
            });
        h2 Sign In Form
        form(method='post', action='/signin')
            p
                label(for='username') username
                input#username(type='text', name='username', placeholder='username', required='true')
            p
                label(for='password') password
                input#password(type='text', name='password', placeholder='password', required='true')
            p
                input#signin(type='submit', name='signin', value='sign in')
                a(href='/signup', title='register') register        

当我到达索引页面时,我看到Uncaught ReferenceError: $ is not defined 在第10行($(document).ready(function()

我做错了什么?整个项目在github上是here

注意:css正在运行(根据public/css/simple.css中的css,颜色变为红色。

另外,我需要更多的只是客户端的jquery,我有大约2-3个js文件,这些文件应该在客户端运行,我怎样才能使它们对客户端视图也可见?

3 个答案:

答案 0 :(得分:2)

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click If class1.chkfieldss(TextBox14.Text, TextBox1.Text, _ TextBox8.Text, TextBox9.Text, _ TextBox10.Text, TextBox11.Text, TextBox12.Text) = False Then If class1.registercust(TextBox14.Text, TextBox1.Text, TextBox8.Text, TextBox9.Text, TextBox10.Text, _ TextBox11.Text, DateTimePicker1.Value, _ TextBox12.Text, ComboBox3.SelectedItem) = False Then MessageBox.Show("REGISTER SUCCESSFULLY", "Welcome", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) Else MessageBox.Show("Something Happen", "Error", MessageBoxButtons.OK, _ MessageBoxIcon.Error) End If Else MessageBox.Show("Complete all fields", "Error", MessageBoxButtons.OK, _ MessageBoxIcon.Error) End If End Sub 更改行:

app.js

app.use(express.static(path.join(__dirname, 'public')));    
app.use('/jquery', express.static(path.join(__dirname, '/public/js')));

这样,可以访问所有css和js文件。

Jade文件(您的代码中app.use(express.static(__dirname + '/public')); 路径错误):

jquery

答案 1 :(得分:1)

你应该使用

script(type="type/javascript",src="/js/jquery.min.js")

摆脱这条线:

app.use('/jquery', express.static(path.join(__dirname, '/public/js')));

答案 2 :(得分:0)

确定与doctype htmlhtml(lang='en')有关,删除它似乎可以使其正常工作。 (有道理,因为它不是HTML?)

新玉视图:

doctype
html
    head
        title #{title}
        link(type='text/css', rel='stylesheet', href='/css/simple.css')
        script(type='text/javascript', src='/js/jquery.min.js')
        script(type='text/javascript', src='/js/testjs.js')
    body
        script.
            $(document).ready(function() {
                console.log('hello form js and jquery')
                helloinfile('adi')
            });
        h2 Sign In Form

        form(method='post', action='/signin')
            p
                label(for='username') username
                input#username(type='text', name='username', placeholder='username', required='true')
            p
                label(for='password') password
                input#password(type='text', name='password', placeholder='password', required='true')
            p
                input#signin(type='submit', name='signin', value='sign in')
                a(href='/signup', title='register') register

我可以看到来自console.log和来自helloinfile的控制台消息。完整代码在github上更新。