连续写入服务器中的多个文件 [英] Consecutive writings to several files in the server
问题描述
(*正如
**********我写的是什么**** ******
在html文件中,我为每个文件添加了一个控制器,以便我可以准确地跟踪在文本区域中哪个文件被更改。然后,我只需要将该文件保存到服务器,而不是所有文件:
< div ng-repeat = 通过$ index跟踪文件中的文件 ng-controller = fileCtrl>
< a ng-click = go(file)> {{file.name}}< / a>
< / div>
控制器:
app.controller('fileCtrl',['$ scope','codeService',function($ scope,codeService){
$ scope。$ watch('file',function(){
codeService.render($ scope.files,$ scope.file);
},true);
}]));
codeService
:
app.service('codeService',['$ http',function($ http){
this.render = function(files ,changedFile){
$ http.post('/ writeFile',changedFile);
};
}]));
路由器:
router.post('/ writeFile',函数(req,res,next){
var file = req.body;
var fs = require('fs');
fs.writeFile( public / tmp / + file.name,file.body,函数(err){
if(err){return console.log(err)};
} );
});
**********我的测试********* *
我的测试表明,textarea中的修改被很好地捕获了,修改后的文件或多或少可以保存到服务器上:它工作得很好对于第一篇,第二篇著作,但随后的著作常常会遇到麻烦。
**********我的问题* *********
有人可以帮助我重新构造代码以使其处理得很好吗?
- 写作的异步调用,这样所有写作都可以很好地(且快速地)进行。
- 带有
防反跳
的文字,这意味着我们可以在每次保存之前稍等片刻。但是当我们有多个文件时,这有点棘手:假设我们可以非常快在文件之间切换,去抖动
和异步保存将如何执行
1.这是 plunker
http://plnkr.co/edit/NerwghZaqcRuoswHYPlq?p=preview ,可以在其中使用反跳功能和异步功能。
2.一旦在文件之间快速切换并键入了不同的文件,只有最后的结果将被发送到服务器
3.不要忘记处理错误,以防您从文件中获取文件BE(在每次对服务器的$ http调用之后添加catch())
4.在Node Js端,请添加Promise库,以简化您的任务。我个人更喜欢Bluebird http://bluebirdjs.com/docs/api/promise.all。 html
var files = [];
for(var i = 0; i< 100; ++ i){
files.push(fs.writeFileAsync( file- + i + .txt,, utf -8));
}
Promise.all(files).then(function(){
console.log(所有文件均已创建);
});
希望有帮助
(* As my previous question has been more or less answered, and the code has evolved, I open a new question. *)
I want to build a simple playground with MEAN stack that mimics plunker: we have a list of files and a textarea on the left hand, and a live preview on the right hand. Note that the files are saved in a temporary folder, and the live preview is an iframe
injected by the files from that temporary folder.
********** what I have written **********
In the html file, I add one controller per file, so that I could track exactly which file is changed in the textarea. Then, I just need to save that file to the server, rather than all the files:
<div ng-repeat="file in files track by $index" ng-controller="fileCtrl">
<a ng-click="go(file)">{{file.name}}</a>
</div>
the controller:
app.controller('fileCtrl', ['$scope', 'codeService', function ($scope, codeService) {
$scope.$watch('file', function () {
codeService.render($scope.files, $scope.file);
}, true);
}]);
the codeService
:
app.service('codeService', ['$http', function ($http) {
this.render = function (files, changedFile) {
$http.post('/writeFile', changedFile);
};
}]);
the router:
router.post('/writeFile', function (req, res, next) {
var file = req.body;
var fs = require('fs');
fs.writeFile("public/tmp/" + file.name, file.body, function (err) {
if (err) { return console.log(err) };
});
});
********** my tests **********
My tests show that the modification in the textarea is well caught, and the modified file can more or less be saved to the server: it works quite well for the 1st, 2nd writings, but it often has trouble for the writings that follow.
********** my questions **********
Could anyone help me re-structure the code to handle well:
- asynchronous calls of writings, such that all the writings are well (and fast) undertaken.
- writings with
debounce
, that means we could wait a little bit before each saving. But it's a little bit tricky when we have several files: assume we could switch among files very fast, how woulddebounce
and asynchronous saving perform in that case?
1. Here is plunker
http://plnkr.co/edit/NerwghZaqcRuoswHYPlq?p=preview for you where you can play with debounce and asynchronous functions.
2. Once you switch between files very fast and type in different files, only the last result will be send to the server
3. Don't forget to handle errors in case you get it from BE (add catch() after each $http call to the server)
4. And on the Node Js side please add promise library that simplify your task. Personally I prefer Bluebird http://bluebirdjs.com/docs/api/promise.all.html
var files = [];
for (var i = 0; i < 100; ++i) {
files.push(fs.writeFileAsync("file-" + i + ".txt", "", "utf-8"));
}
Promise.all(files).then(function() {
console.log("all the files were created");
});
Hope it helps
这篇关于连续写入服务器中的多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!