我想從我的客戶那里得到一個請求。讀取檔案并給我作為回應。我正在使用 xhr 來做到這一點。每 1 秒將發送一個請求。所有請求都在一個名為 XHRS 的陣列中。這就是我在說什么:`
var XHRS = [];
var i = 0;
SendRequest();
function SendRequest() {
XHRS[i] = new XMLHttpRequest();
XHRS[i].onload = function(){
var ChatData = XHRS[i].responseText.split("\n");
document.getElementById("ServerMessages").innerHTML = "";
for (var j in ChatData) {
var parent = document.getElementById("ServerMessages");
var el = document.createElement("li");
el.textContext = ChatData[j];
parent.appendChild(el);
}
debugger;
i ;
setTimeout(SendRequest, 1000);
}
XHRS[i].open("GET", "/read?url=messages.txt", false);
XHRS[i].send();
}
正如你在這里看到的。我曾經i拆分請求。我使用 onl oad 來查看發送請求是否完成。在服務器中,我收到這樣的請求:
app.get('/read', function(request, response) {
console.log("readFileUrl : " request.query.url);
response.type("html");
response.send(getReadFile(request.query.url));
});
function getReadFile(url){
var Data;
readFile(url);
async function readFile(filePath) {
try {
var data = await fs.readFile(filePath);
Data = data;
console.log(data.toString());
} catch (error) {
console.error(`Got an error trying to read the file: ${error.message}`);
}
}
return Data;
}
` 我的問題是 responseText 為空。當我在服務器中發送回應時。我檢查了 getReadFile 是否回傳 null 但它有文本。我應該怎么辦?
有趣的是,當我使用 response.write 和 response.end 時它會出錯。它得到這個錯誤:
TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer or Uint8Array. Received undefined
at new NodeError (node:internal/errors:371:5)
at write_ (node:_http_outgoing:742:11)
at ServerResponse.write (node:_http_outgoing:707:15)
at E:\alireza\vscode programs\node.js chat without socket.io\index.js:11:14
at Layer.handle [as handle_request] (E:\alireza\vscode programs\node.js chat without socket.io\node_modules\express\lib\router\layer.js:95:5)
at next (E:\alireza\vscode programs\node.js chat without socket.io\node_modules\express\lib\router\route.js:144:13)
at Route.dispatch (E:\alireza\vscode programs\node.js chat without socket.io\node_modules\express\lib\router\route.js:114:3)
at Layer.handle [as handle_request] (E:\alireza\vscode programs\node.js chat without socket.io\node_modules\express\lib\router\layer.js:95:5)
at E:\alireza\vscode programs\node.js chat without socket.io\node_modules\express\lib\router\index.js:284:15
at Function.process_params (E:\alireza\vscode programs\node.js chat without socket.io\node_modules\express\lib\router\index.js:346:12)
wfveragerfgesgerg
readFileUrl : messages.txt
TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer or Uint8Array. Received undefined
at new NodeError (node:internal/errors:371:5)
at write_ (node:_http_outgoing:742:11)
at ServerResponse.write (node:_http_outgoing:707:15)
at E:\alireza\vscode programs\node.js chat without socket.io\index.js:11:14
at Layer.handle [as handle_request] (E:\alireza\vscode programs\node.js chat without socket.io\node_modules\express\lib\router\layer.js:95:5)
at next (E:\alireza\vscode programs\node.js chat without socket.io\node_modules\express\lib\router\route.js:144:13)
at Route.dispatch (E:\alireza\vscode programs\node.js chat without socket.io\node_modules\express\lib\router\route.js:114:3)
at Layer.handle [as handle_request] (E:\alireza\vscode programs\node.js chat without socket.io\node_modules\express\lib\router\layer.js:95:5)
at E:\alireza\vscode programs\node.js chat without socket.io\node_modules\express\lib\router\index.js:284:15
at Function.process_params (E:\alireza\vscode programs\node.js chat without socket.io\node_modules\express\lib\router\index.js:346:12)
uj5u.com熱心網友回復:
我認為您忘記將 await/async 部分放入您的函式中
app.get('/read', async function(request, response) {
console.log("readFileUrl : " request.query.url);
response.type("html");
response.send(await getReadFile(request.query.url));
});
async function getReadFile(url){
var Data;
await readFile(url);
async function readFile(filePath) {
try {
var data = await fs.readFile(filePath);
Data = data;
console.log(data.toString());
} catch (error) {
console.error(`Got an error trying to read the file: ${error.message}`);
}
}
return Data;
}
你可以簡單地做到這一點:
app.get('/read', function(request, response) {
console.log("readFileUrl : " request.query.url);
response.type("html");
response.send(fs.readFileSync(request.query.url));
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/521271.html
