JS 文件不 link 与 Pug
JS file doesn't link with Pug
我的 js 文件无法 link 显示到我的哈巴狗浏览器控制台
The script from “http://localhost:3000/script.js” was loaded even though its MIME type (“text/html”) is not a valid JavaScript MIME type.
2 signin
Loading failed for the <script> with source “http://localhost:3000/script.js”. signin:1:1
我试过用脚本标签来做这件事,但我猜我的 js 代码有一些错误,除非代码在不同的文件中,否则我看不到这些错误。
这是我的哈巴狗文件
script(type='text/javascript' src="http://code.jquery.com/jquery-latest.js")
script( type= 'text/javascript',src="./script.js")
form( id='formSignIn')
div.form-group
label(for='name') Id:
input#name.form-control(type='text', placeholder='id' name='name')
div.form-group
label(for='pw') Password:
input#password.form-control(type='password', name='password')
button.btn.btn-primary(type='submit', id='submit') Sign In
这是我的js文件
$(document).ready(function(){
console.log("hi");
var name,password;
// $("#submit").click(function(){
name= $("#name").val();
password=$("#password").val();
console.log("$$$$$$$$$$$$$", name, password)
$.post("/login", {name: name, password: password} ,function(data){
console.log("AJAx");
});
console.log("@@@@@@@@@@@@@@@@@");
localStorage.setItem('user',name);
}
你的 index.js 文件在哪里(或者 app.js 你的主服务器文件叫什么)?
您需要设置 public 文件夹,并将 script.js 文件放在那里。
EXAMPLE 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}`))
您看到 app.use(express.static('public')) 行了吗?这会将您的服务器设置为使用您的 public 文件夹。您在根文件夹中创建一个 public 文件夹。将您的 script.js 文件放入该 public 文件夹中。
现在在你的 pug 文件中,你可以加载带有标签的 script.js 文件
script(src='/script.js')
您不需要将其设置为“/public/script.js”,因为您已经将 public 文件夹设置为源。你只需要指向文件,就是它只是'/script.js'
我的 js 文件无法 link 显示到我的哈巴狗浏览器控制台
The script from “http://localhost:3000/script.js” was loaded even though its MIME type (“text/html”) is not a valid JavaScript MIME type.
2 signin
Loading failed for the <script> with source “http://localhost:3000/script.js”. signin:1:1
我试过用脚本标签来做这件事,但我猜我的 js 代码有一些错误,除非代码在不同的文件中,否则我看不到这些错误。
这是我的哈巴狗文件
script(type='text/javascript' src="http://code.jquery.com/jquery-latest.js")
script( type= 'text/javascript',src="./script.js")
form( id='formSignIn')
div.form-group
label(for='name') Id:
input#name.form-control(type='text', placeholder='id' name='name')
div.form-group
label(for='pw') Password:
input#password.form-control(type='password', name='password')
button.btn.btn-primary(type='submit', id='submit') Sign In
这是我的js文件
$(document).ready(function(){
console.log("hi");
var name,password;
// $("#submit").click(function(){
name= $("#name").val();
password=$("#password").val();
console.log("$$$$$$$$$$$$$", name, password)
$.post("/login", {name: name, password: password} ,function(data){
console.log("AJAx");
});
console.log("@@@@@@@@@@@@@@@@@");
localStorage.setItem('user',name);
}
你的 index.js 文件在哪里(或者 app.js 你的主服务器文件叫什么)?
您需要设置 public 文件夹,并将 script.js 文件放在那里。
EXAMPLE 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}`))
您看到 app.use(express.static('public')) 行了吗?这会将您的服务器设置为使用您的 public 文件夹。您在根文件夹中创建一个 public 文件夹。将您的 script.js 文件放入该 public 文件夹中。
现在在你的 pug 文件中,你可以加载带有标签的 script.js 文件
script(src='/script.js')
您不需要将其设置为“/public/script.js”,因为您已经将 public 文件夹设置为源。你只需要指向文件,就是它只是'/script.js'