代码说明:当用户访问特定 url 时,我正在返回特定的 HTML 文件:
const http = require('http');
const fs = require('fs');
fs.readFile('./funkcionalnosti-streznika.html', function(err1, html1) {
fs.readFile('./posebnosti.html', function(err2, html2) {
if (err1 || err2) {
throw new Error();
}
http.createServer(function(req, res) {
if (req.url == '/funkcionalnosti-streznika') {
res.write(html1);
res.end();
}
if (req.url == '/posebnosti') {
res.write(html2)
res.end();
} else {
res.write('random');
res.end();
}
}).listen(8080)
})
});
Run Code Online (Sandbox Code Playgroud)
在终端上,当我访问 localhost:8080/funkcionalnosti-streznika 时出现此错误:
events.js:288
throw er; // Unhandled 'error' event
^
Error [ERR_STREAM_WRITE_AFTER_END]: write after end
at write_ (_http_outgoing.js:637:17)
at ServerResponse.write (_http_outgoing.js:629:15)
at …Run Code Online (Sandbox Code Playgroud)