I wrote a function to read and replace words by words written in a form in angularjs. I would like to know your opinions to optimize itKnowing that I have to perform the changes in two files. function openFile(infile) {                fs.open(infile, 'r', function (err, fd) {                    changeFile(fd);                });            }        function changeFile(fd) {            var out = '';            var buf = new Buffer(1024);            buf.fill(0);            try {                fs.read(fd, buf, 0, 1024, null, function (err, bytesRead, buffer) {                    if (bytesRead === 0) {                        writeFile(out);                        return;                    }                    var str = buffer.toString('utf8', 0, bytesRead);                    for (var i in req.body.tpl.title) {                        str = str.replace('${' + req.body.tpl.title[i].id + '}', req.body.tpl.title[i].value);                    }                    for (var i in req.body.tpl.content) {                        str = str.replace('${' + req.body.tpl.content[i].id + '}', req.body.tpl.content[i].value);                    }                    for (var i in req.body.tpl.image) {                        str = str.replace('${' + req.body.tpl.image[i].id + '}', '../../../' + req.body.tpl.image[i].path);                    }                    for (var i in req.body.tpl.video) {                        str = str.replace('${' + req.body.tpl.video[i].id + '}', '../../../' + req.body.tpl.video[i].path);                    }                    out += str;                    changeFile(fd);                });            } catch (err) {                console.error(err);            }        }        function writeFile(str) {            fs.writeFile(infile, str, function (err, written, buffer) {                fs.rename(infile, infile);            });        }Thanks for you solution 
Nodejs Read and replace into file
javascript;node.js;angular.js