将JavaScript文件链接到HTML文件时遇到了一个大问题。
我的HTML代码:
<!DOCTYPE html>
<html>
<head>
<title>Express App</title>
</head>
<body>
<h1>GET Requests</h1><br/>
<input type="number" name="id" id="id-input>
<button type="submit" onclick="findPerson()">Submit</button>
<script src="./index.js" type="text/javascript"></script>
</body>
</html>
这段代码看起来还不错,实际上,这是我的JavaScript代码:
const express = require('express');
const app = express();
const server = 'http://localhost:3000';
app.connect(server);
app.get('/', (req, res) => {
res.sendFile(__dirname + "/" + "index.html");
});
const findPerson = () => {
var ID = window.document.getElementById('id-
input').value;
console.log(ID);
};
app.listen(8080, () => {
console.log('App is listening on port 8080');
});
现在,由于某种原因,当我检查页面并转到控制台时,它会说:
Refused to execute script from
'http://localhost:8080/index.js' because
its MIME type ('text/html') is not executable, and
strict MIME type checking is enabled.
还有这个:
Failed to load resource: the server responded with
a status of 404 (Not Found) index.js:1
答案 0 :(得分:3)
我可以看到您的代码有两个错误: