我设法通过请求模块找到了一个可行的解决方案。它可以满足我的需要,而且它似乎是任何 Web 应用程序的全面工具。这是有效的代码:
var userImageProp = {};
if ((profile.picture) && (profile.picture.data.is_silhouette == false)) {
var pictureURL = 'https://graph.facebook.com/'+ profile.id +'/picture?type=large';
// Determine file name.
var filename = profile.picture.data.url.replace(/^.*[\\\/]/, '');
// Precreate stream and define callback.
var picStream = fs.createWriteStream('/tmp/'+filename);
picStream.on('close', function() {
console.log('Downloaded '+filename);
userImageProp.path = '/tmp/'+filename;
finishSave(user, userImageProp);
});
// Get and save file.
request(pictureURL).pipe(picStream);
} else {
userImageProp.path = config.root + '/defaults/img/faceless_'+user.gender.toLowerCase()+'.png';
finishSave(user, userImageProp);
}
function finishSave(user, userImageProp) {
user.attach('userImage', userImageProp, function(err) {
console.dir(err);
if (err) { return done(new restify.InternalError(err)); }
user.save(function (err, user) {
if (err) { return done(new restify.InternalError(err)); }
// Saved successfully. Return user for login, and forward client to complete user creation.
return done(null, user, '/#/sign-up/facebook/save');
});
});
}
感谢这些线程帮助我提出了这个解决方案: