We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
实现读取文件,之前的readfile可实现,如下:
readfile
var server = http.createServer(function(req, res) { fs.readFile(__dirname + '/cont/vue.html', function(err, data) { if(err) { res.statusCode = 500; res.end(String(err)); } else { res.end(data); } }); });
上方法存在以下问题:
文件内容都直接缓存在内存中,影响并发速度
优化方法:
var server = http.createServer(function(req, res) { var stream = fs.createReadStream(__dirname + '/cont/vue.html'); stream.pipe(res); }) server.listen(8000); console.log('正在打开http://localhost:8000/')
使用stream实现优化,且代码简洁.
stream
进一步,实现gzip
gzip
var http = require('http'); var fs = require('fs'); var zlib = require('zlib'); var server = http.createServer(function(req, res) { res.writeHead(200, {'content-encoding': 'gzip'}); var stream = fs.createReadStream(__dirname + '/cont/vue.html'); stream.pipe(zlib.createGzip()).pipe(res); }); server.listen(8000); console.log('正在打开http://localhost:8000/')
完整代码及其他相关node代码,见https://github.com/onvno/node-module
The text was updated successfully, but these errors were encountered:
No branches or pull requests
实现读取文件,之前的
readfile
可实现,如下:上方法存在以下问题:
优化方法:
使用
stream
实现优化,且代码简洁.进一步,实现
gzip
完整代码及其他相关node代码,见https://github.com/onvno/node-module
The text was updated successfully, but these errors were encountered: