私は、deploy.jsというファイルを作成しました。このファイルは、そのAPIを介してgithubにフォルダのすべてのファイルをアップロードします。アップロードするユーザーとフォルダのaccessTokenという2つの引数が必要です。私はパスポート - githubを介してアクセストークンを取得します。私は主なapp.jsファイルを持っており、deploy.jsは変数deployによって利用可能です。 App.jsには、deploy機能を呼び出すルート/what
があります。問題は、次のコードが機能することです。応答が送信された後に非同期機能が正しく実行されない
app.get('/what', function(req, res) {
//req.user.token contains the oauth token of the user
deploy(req.user.token, folder, function() {
res.send("completed");
});
});
しかし、res.send()
をコールバックの外に置くと、完全には実行されません。
app.get('/what', function(req, res) {
deploy(req.user.token, folder, function() {
console.log("deploy completed");
});
res.send("Work being done");
})
ここに私が書いた私のdeploy.jsファイルがあります。私はネイティブgithub API.
var distHelper = require('./dist.js');
var fs = require('fs');
var path = require('path');
var github = require('github');
var gh = new github();
module.exports = function(accessToken, folder, callback) {
//Authenticating using the oauth accessToken received above
gh.authenticate({ type:"oauth", token: accessToken});
var fullPath = distHelper.distPath + "/" + folder;
var eventName = folder.substr(folder.search("/") + 1);
//the below function returns a list of all files in a directory recursively
var walk = function(dir, done) {
var results = [];
fs.readdir(dir, function(err, list) {
if (err) return done(err);
var i = 0;
(function next() {
var file = list[i++];
if (!file) return done(null, results);
file = dir + '/' + file;
fs.stat(file, function(err, stat) {
if (stat && stat.isDirectory()) {
walk(file, function(err, res) {
results = results.concat(res);
next();
});
} else {
results.push(file);
next();
}
});
})();
});
};
//creating a repo
gh.repos.create({name: 'b', auto_init: 1}, function(err, response) {
if(err) {
console.log(err);
}
walk(fullPath, function(err, results) {
if(err) {
console.log(err);
}
//Base64 encoding of a file
function encode(file) {
var bitmap = fs.readFileSync(file);
return new Buffer(bitmap).toString('base64');
}
//Function which uploads file to the github one by one
function doOne() {
var elem = results.shift();
var fileName = elem.substr(elem.search(eventName)+eventName.length + 1);
console.log(fileName);
gh.repos.createFile({owner: "princu7", repo: "b", path: fileName, message: "please work", content:encode(elem)}, function(err, res) {
if(err) {
console.log(err);
}
else {
console.log("got response");
}
if(results.length > 0) {
doOne();
}
else {
// when all files are finished uploading, then the below function is called
callback();
}
});
}
doOne();
});
});
};
周りのノードのラッパーとしてnpm-github
ライブラリを使用していた私は、理由のために探して壁に頭を打ちましたが、私はいずれかを見つけることができませんでした。それは閉鎖に関連するかもしれませんが、私は本当に確実ではありません。誰かが助けてくれたら本当に感謝しています。ありがとう。
node.js'は非同期で 'ので。 –
これは 'I/O'であり、' async'でなければならず、あなたは 'deploy'関数の中であなたの応答を送る必要があります。詳細については[こちら](http://stackoverflow.com/questions/41401902/send-variables-to-layout/41402223#41402223)をご覧ください。 –
私は知っていますが、私があなたが指摘していたことを理解していませんでした。最初のケースでは、正常に動作します。 2番目のケースでは、最初に実行された後、ある時点でハングします。 res.send()の後であっても、deploy関数は引き続き実行する必要がありますか?誰かが私を修正してください。 –