连续写入服务器中的多个文件 [英] Consecutive writings to several files in the server

查看:22
本文介绍了连续写入服务器中的多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(* 因为

********** 我写的东西************

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.

在 html 文件中,我为每个文件添加一个控制器,以便我可以准确跟踪文本区域中更改的文件.然后,我只需要将该文件保存到服务器,而不是所有文件:

********** what I have written **********
<a ng-click="go(file)">{{file.name}}</a>

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:

控制器:

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', 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 **********

我的测试表明textarea中的修改被很好的捕捉到,修改后的文件或多或少可以保存到服务器:对于第一、第二次写的效果很好,但经常遇到麻烦后面的文章.

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:

  1. 写作的异步调用,这样所有的写作都很好(并且快速)进行.
  2. 使用debounce 编写,这意味着我们可以在每次保存之前稍等片刻.但是当我们有多个文件时,这有点棘手:假设我们可以非常快地在文件之间切换,那么在这种情况下,debounce 和异步保存将如何执行?
  1. asynchronous calls of writings, such that all the writings are well (and fast) undertaken.
  2. 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 would debounce and asynchronous saving perform in that case?

推荐答案


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


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");
});

希望能帮到你

这篇关于连续写入服务器中的多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆