我從 Node.js 開始,我的第一個程序已經(jīng)有問題了。下面是我正在使用的代碼。索引.html:<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>Random Temperatures</title> </head> <body> <input type="text" id="tb" name="tb" /> <input type="button" value="Random Number!" id="myButton" name="myButton"/> <script src="client.js"></script></body></html>客戶端.js:const textBox = document.getElementById('tb');const button = document.getElementById('myButton');button.addEventListener('click', function(e) { var rnd = Math.floor(Math.random() * 100); textBox.value = rnd;});服務(wù)器.js:var app = require('http').createServer(response);var fs = require('fs');app.listen(8080);console.log("App running…");function response(req, res) { fs.readFile(__dirname + '/public/index.html', function (err, data) { if (err) { res.writeHead(500); return res.end('Failed to load file index.html'); } res.writeHead(200); res.end(data); });}當(dāng)我啟動應(yīng)用程序時,我會轉(zhuǎn)到瀏覽器,出現(xiàn)文本框和按鈕。但是在瀏覽器控制臺中,我收到了這些錯誤:client.js:1 Uncaught SyntaxError: Unexpected token <ContentScript.js:112 onResRdy 中的異常:TypeError:無法讀取未定義的屬性“htmlRes”localhost/:1 Unchecked runtime.lastError: 無法建立連接。接收端不存在。我想我的問題是 3 個文件之間的鏈接,但我嘗試了幾件事,但無法解決問題。我確定這是一個愚蠢的錯誤,但請?jiān)徫也艅倓傞_始。有什么建議嗎?
鏈接 index.html client.js 和 server.js
www說
2021-09-17 21:00:01