为什么浏览器在使用 createReadStream() 时一直加载 Node.js
Why Does Browser Keep Loading When Using createReadStream() Node.js
Node.js 的新手 我明白 createReadStream()
函数比 readFile()
的性能更好,因为 createReadStream()
在卡盘中读取和写入数据,而 [=13] =] 首先阅读全部内容。因此,如果文件很大,readFile()
函数可能需要更长的时间才能进一步处理数据。因此,我选择使用 createReadStream()
函数创建服务器,如下所示。
// Create a server with fs.createReadStream(), better performance and less memory usage.
http.createServer( function (request, response) {
// Parse the request containing file name
var pathname = url.parse(request.url).pathname;
// Create a readable stream.
var readerStream = fs.createReadStream(pathname.substr(1));
// Set the encoding to be UTF8.
readerStream.setEncoding('UTF8');
// Handle stream events --> data, end and error
readerStream.on('data', function(chunk) {
// Page found
// HTTP Status: 200 : OK
// Content Type: text/plain
response.writeHead(200, {'Content-type': 'text/html'});
// Write the content of the file to response body.
response.write(chunk);
console.log('Page is being streamed...');
});
readerStream.on('end', function() {
console.log('Page is streamed and emitted successfully.');
});
readerStream.on('error', function(err) {
// HTTP Status: 404 : NOT FOUND
// Content Type: text/plain
response.writeHead(404, {'Content-type': 'text/html'});
console.log('Page streaming error: ' + err);
});
console.log('Code ends!');
}).listen(8081);
// Console will print the message
console.log('Server running at http://127.0.0.1:8081/');
我的 .html
或 .txt
文件包含三行短文本。启动我的服务器后,我通过 http://127.0.0.1:8081/index.html
访问我的网页。一切正常,index.html
的内容在浏览器上得到回显。
但是在浏览器的选项卡上,加载程序图标一直在转动,好像一直在加载大约 1 分钟。
Node.js 服务器正常吗?图标是否一直在转动,但对服务器没有任何成本?还是我错过了什么,图标不应该一直转动?
您似乎没有结束回复。浏览器可能认为请求未完成,因此继续 "load"。
如果您查看开发人员控制台中的“网络”选项卡,您可能会看到请求尚未完成。
你应该发送 response.end()
This method signals to the server that all of the response headers and body have been sent; that server should consider this message complete. The method, response.end(), MUST be called on each response.
我相信你应该在写完 head 之后在 readerStream.on('end'
和 readerStream.on('error'
回调中调用 response.end()
。这将告诉浏览器请求已完成,它可以停止加载操作。
Node.js 的新手 我明白 createReadStream()
函数比 readFile()
的性能更好,因为 createReadStream()
在卡盘中读取和写入数据,而 [=13] =] 首先阅读全部内容。因此,如果文件很大,readFile()
函数可能需要更长的时间才能进一步处理数据。因此,我选择使用 createReadStream()
函数创建服务器,如下所示。
// Create a server with fs.createReadStream(), better performance and less memory usage.
http.createServer( function (request, response) {
// Parse the request containing file name
var pathname = url.parse(request.url).pathname;
// Create a readable stream.
var readerStream = fs.createReadStream(pathname.substr(1));
// Set the encoding to be UTF8.
readerStream.setEncoding('UTF8');
// Handle stream events --> data, end and error
readerStream.on('data', function(chunk) {
// Page found
// HTTP Status: 200 : OK
// Content Type: text/plain
response.writeHead(200, {'Content-type': 'text/html'});
// Write the content of the file to response body.
response.write(chunk);
console.log('Page is being streamed...');
});
readerStream.on('end', function() {
console.log('Page is streamed and emitted successfully.');
});
readerStream.on('error', function(err) {
// HTTP Status: 404 : NOT FOUND
// Content Type: text/plain
response.writeHead(404, {'Content-type': 'text/html'});
console.log('Page streaming error: ' + err);
});
console.log('Code ends!');
}).listen(8081);
// Console will print the message
console.log('Server running at http://127.0.0.1:8081/');
我的 .html
或 .txt
文件包含三行短文本。启动我的服务器后,我通过 http://127.0.0.1:8081/index.html
访问我的网页。一切正常,index.html
的内容在浏览器上得到回显。
但是在浏览器的选项卡上,加载程序图标一直在转动,好像一直在加载大约 1 分钟。
Node.js 服务器正常吗?图标是否一直在转动,但对服务器没有任何成本?还是我错过了什么,图标不应该一直转动?
您似乎没有结束回复。浏览器可能认为请求未完成,因此继续 "load"。
如果您查看开发人员控制台中的“网络”选项卡,您可能会看到请求尚未完成。
你应该发送 response.end()
This method signals to the server that all of the response headers and body have been sent; that server should consider this message complete. The method, response.end(), MUST be called on each response.
我相信你应该在写完 head 之后在 readerStream.on('end'
和 readerStream.on('error'
回调中调用 response.end()
。这将告诉浏览器请求已完成,它可以停止加载操作。