I want to send multiple files into git repository using nodejs. its moving single file only. when looping its throwing error like Cannot set headers after they are sent to the client. below is my code. suggest me where I did wrong.
var base64 = require('file-base64');
for (var i in files) {
console.log('file load: ' + files[i]);
base64.encode('/opt/zipoutput/' + files[i], function (err, base64String) {
convertval = base64String;
var dataObj = {
"branch": "master",
"commit_message": "dump message",
"actions": [
{
"action": "create",
"file_path": files[i],
"content": convertval,
"encoding": "base64"
}
]
};
var filesrc = files;
var Client = require('node-rest-client').Client;
var client = new Client()
var args = {
data: dataObj,
headers: { 'Content-Type': 'application/json', 'PRIVATE-TOKEN': 'xxxxxxxxxxxxxxxx' },
};
client.post("https://gitlab.com/api/v4/projects/77/repository/commits", args, function (data, response) {
fs.rename('/opt/zipoutput/'+files[i], '/opt/sentFiles/'+files[i], function(err) {
if (err) throw err;
console.log("args passed:",args);
console.log("successfully moved into Sent file dirctory");
});
console.log("file send: True");
res.send("true");
});
});