连续写入服务器中的文件很慢
Consecutive writings to files in the server are slow
我想用模仿 plunker 的 MEAN 堆栈构建一个简单的游乐场:我们在左侧有一个文件列表和一个文本区域,在右侧有一个实时预览。请注意,文件保存在一个临时文件夹中,实时预览是由该临时文件夹中的文件注入的 iframe
。
我已经编码了一些东西。在前端,控制器监视文本区域中文件的修改;每次有变化,都会调用render
,它会发送一个$http.post
来保存所有文件的新版本。
app.controller('Ctrl', ['$scope', 'codeService', function ($scope, codeService) {
...
$scope.$watch('files', function () {
codeService.render($scope.files)
}, true);
}]);
app.service('codeService', ['$http', function ($http) {
this.render = function (files) {
...
var arrayLength = files.length;
for (var i = 0; i < arrayLength; i++) {
$http.post('/writeFile', files[i]);
}
}
}
在后端:
router.post('/writeFile', function (req, res, next) {
console.log("router.post /writeFile");
var file = req.body;
var fs = require('fs');
fs.writeFileSync("public/tmp/" + file.name, file.body);
});
我的测试表明,对于第一次修改,它确实写入了服务器中的文件。但是,对于连续的修改,第2次及以后的写入可能需要20秒以上EACH。
有谁知道是什么让写作变慢了(除了第一个)?
另外,我应该异步调用$http.post('/writeFile', files[i])
还是写router.post('/writeFile'...
?
编辑 1:
我也想知道在同步函数(即render
)? 我应该使 render
异步吗?):
app.service('codeService', ['$http', function ($http) {
this.render = function (files) {
...
var arrayLength = files.length;
for (var i = 0; i < arrayLength; i++) {
$http.post('/writeFile', files[i]);
}
}
}
当我在我的代码中看到其他http请求时,时尚往往是这样的
o.create = function (post) {
return $http.post('/posts', post, {
headers: { Authorization: 'Bearer ' + auth.getToken() }
}).success(function (data) {
o.posts.push(data)
});
};
您可以尝试重构您的代码并包括以下内容:
1) 将 watcher 封装到去抖功能中。https://lodash.com/docs/4.17.4#debounce
$scope.$watch('files', _.debounce(function () {
codeService.render($scope.files)
}, 1000), true);
防止无用的调用
2)使用writeFile
代替writeFileSync
fs.writeFile("public/tmp/" + file.name, file.body, (err) => {
if (err) throw err;
console.log('It\'s saved!');
});
NodeJs 在异步函数中的强大功能,尽量避免在代码中调用同步。
我想用模仿 plunker 的 MEAN 堆栈构建一个简单的游乐场:我们在左侧有一个文件列表和一个文本区域,在右侧有一个实时预览。请注意,文件保存在一个临时文件夹中,实时预览是由该临时文件夹中的文件注入的 iframe
。
我已经编码了一些东西。在前端,控制器监视文本区域中文件的修改;每次有变化,都会调用render
,它会发送一个$http.post
来保存所有文件的新版本。
app.controller('Ctrl', ['$scope', 'codeService', function ($scope, codeService) {
...
$scope.$watch('files', function () {
codeService.render($scope.files)
}, true);
}]);
app.service('codeService', ['$http', function ($http) {
this.render = function (files) {
...
var arrayLength = files.length;
for (var i = 0; i < arrayLength; i++) {
$http.post('/writeFile', files[i]);
}
}
}
在后端:
router.post('/writeFile', function (req, res, next) {
console.log("router.post /writeFile");
var file = req.body;
var fs = require('fs');
fs.writeFileSync("public/tmp/" + file.name, file.body);
});
我的测试表明,对于第一次修改,它确实写入了服务器中的文件。但是,对于连续的修改,第2次及以后的写入可能需要20秒以上EACH。
有谁知道是什么让写作变慢了(除了第一个)?
另外,我应该异步调用$http.post('/writeFile', files[i])
还是写router.post('/writeFile'...
?
编辑 1:
我也想知道在同步函数(即render
)? 我应该使 render
异步吗?):
app.service('codeService', ['$http', function ($http) {
this.render = function (files) {
...
var arrayLength = files.length;
for (var i = 0; i < arrayLength; i++) {
$http.post('/writeFile', files[i]);
}
}
}
当我在我的代码中看到其他http请求时,时尚往往是这样的
o.create = function (post) {
return $http.post('/posts', post, {
headers: { Authorization: 'Bearer ' + auth.getToken() }
}).success(function (data) {
o.posts.push(data)
});
};
您可以尝试重构您的代码并包括以下内容:
1) 将 watcher 封装到去抖功能中。https://lodash.com/docs/4.17.4#debounce
$scope.$watch('files', _.debounce(function () {
codeService.render($scope.files)
}, 1000), true);
防止无用的调用
2)使用writeFile
代替writeFileSync
fs.writeFile("public/tmp/" + file.name, file.body, (err) => {
if (err) throw err;
console.log('It\'s saved!');
});
NodeJs 在异步函数中的强大功能,尽量避免在代码中调用同步。