我写了一个名为deploy.js的文件,它通过API将文件夹的所有文件上传到github。它需要两个参数 - 您要上载的用户和文件夹的accessToken。我通过passport-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文件。我使用npm-github
库作为本地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();
});
});
};
我把头撞在墙上寻找原因,但我还没有找到任何原因。它可能与封闭有关,但我不太确定。如果有人愿意,我真的很感激。感谢。
答案 0 :(得分:1)
您应该使用websocket
如下:
app.js
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
socket.on('doplyment', (msg) => {
socket.emit('deploying', 'I will notify you after deployment');
setTimeout(function(){
socket.emit('doployed', 'your request done.')
}, 3000)
});
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
client.js
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script>
var socket = io();
socket.on('connect', function(){
socket.emit('doployment', 'I am wating');
socket.on('deploying', function(mas){
console.log(mas);
});
socket.on('deployed', function (msg) {
console.log(msg);
});
})
</script>