无法将我的 js 文件链接到我的哈巴狗浏览器控制台显示
\n\nThe script from \xe2\x80\x9chttp://localhost:3000/script.js\xe2\x80\x9d was loaded even though its MIME type (\xe2\x80\x9ctext/html\xe2\x80\x9d) is not a valid JavaScript MIME type.\n2 signin\nLoading failed for the <script> with source \xe2\x80\x9chttp://localhost:3000/script.js\xe2\x80\x9d. signin:1:1\nRun Code Online (Sandbox Code Playgroud)\n\n我已经尝试使用脚本标签执行此操作,但我猜我的 js 代码有一些错误,除非代码位于不同的文件中,否则我看不到这些错误。
\n\n这是我的哈巴狗文件
\n\nscript(type=\'text/javascript\' src="http://code.jquery.com/jquery-latest.js")\nscript( type= \'text/javascript\',src="./script.js")\n\n\nform( id=\'formSignIn\')\n div.form-group\n label(for=\'name\') Id:\n input#name.form-control(type=\'text\', placeholder=\'id\' name=\'name\')\n div.form-group\n label(for=\'pw\') Password:\n input#password.form-control(type=\'password\', name=\'password\')\n button.btn.btn-primary(type=\'submit\', id=\'submit\') Sign In\nRun Code Online (Sandbox Code Playgroud)\n\n这是我的js文件
\n\n$(document).ready(function(){\n console.log("hi");\n\n var name,password;\n // $("#submit").click(function(){\n name= $("#name").val();\n password=$("#password").val();\n console.log("$$$$$$$$$$$$$", name, password)\n $.post("/login", {name: name, password: password} ,function(data){\n console.log("AJAx");\n });\n console.log("@@@@@@@@@@@@@@@@@");\n localStorage.setItem(\'user\',name);\n }\nRun Code Online (Sandbox Code Playgroud)\n
你的index.js文件在哪里(或者app.js,无论你的主服务器文件叫什么)?
您需要设置公共文件夹,并将 script.js 文件放入其中。
示例 index.js 文件(或作为服务器运行的主文件)
const express = require('express')
const path = require('path')
const app = express()
const PORT = process.env.PORT || 3001
// static folder
app.use(express.static('public'))
// load view engine
// app.set('views', path.join(__dirname, 'view'))
app.set('view engine', 'pug')
// listening
app.listen(PORT, console.log(`Server started on port ${PORT}`))
Run Code Online (Sandbox Code Playgroud)
您看到 app.use(express.static('public')) 行了吗?这会将您的服务器设置为使用您的公用文件夹。您在根文件夹中创建一个公用文件夹。将 script.js 文件放入该公共文件夹中。
现在在你的 pug 文件中,你可以使用标签加载 script.js 文件
script(src='/script.js')
您不需要将其设置为“/public/script.js”,因为您已经将公共文件夹设置为源。你只需要指向该文件,它只是“/script.js”
| 归档时间: |
|
| 查看次数: |
2606 次 |
| 最近记录: |