Которому проистекает эта ошибка в NodeJS

я - что-то новое в NodeJS и у меня есть эта ошибка. Во что возможно быть должным?

Error: Can't set headers after they are sent.
    at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:356:11)
    at ServerResponse.header (/Users/santiago/Documents/license/node_modules/express/lib/response.js:719:10)
    at ServerResponse.send (/Users/santiago/Documents/license/node_modules/express/lib/response.js:164:12)
    at ServerResponse.json (/Users/santiago/Documents/license/node_modules/express/lib/response.js:250:15)
    at /Users/santiago/Documents/license/license-server/app/routes/apiLicenses.js:32:10
    at DestroyableTransform.<anonymous> (/Users/santiago/Documents/license/license-server/app/models/licenses.js:52:4)
    at emitOne (events.js:96:13)
    at DestroyableTransform.emit (events.js:188:7)
    at /Users/santiago/Documents/license/node_modules/gh-api-stream/index.js:55:30
    at /Users/santiago/Documents/license/node_modules/got/index.js:186:6

Код:

'use strict';

var fs      = require('fs');
var path    = require('path');
var api     = require('gh-api-stream');
var async   = require('async');
var _       = require('lodash');

var PackageJSON             = require('./packageJson');
var retrieveLicensesTree    = require('../utils/retrieveLicensesTree');

function Licenses(repo, settings){
    this.repo = repo;
    this.licenses = null;
    this.cachedFile = path.join(settings.cachePath, repo +'.json');
    this.settings = settings;
}

Licenses.prototype = {

    isCached: function(){
        return fs.existsSync(this.cachedFile);
    },

    getFromCache: function(response){
        response.sendFile(this.cachedFile);
    },

    getFromGithub: function(allRepos, callback){
        var self = this;

        var apiRequest = api('/repos/' + self.settings.org + '/' + self.repo + '/contents/package.json', {
            token: self.settings.gitToken
        });

        apiRequest.on('data', function(response) {
            self.reposParsed = JSON.stringify(allRepos);

            var packageJSON = new PackageJSON(self.settings, self.reposParsed);
            packageJSON.parseFromString(response.content, function(){
                self.getLicenseTree(packageJSON, function(err){
                    if(err)
                        return callback(err, null);

                    self.licenses = packageJSON.list;

                    callback(null, self.licenses);
                });
            });
        });

        apiRequest.on('error', function(err) {
            console.log(err);
            // console.log('Sorry, this license not found');
            callback(err, null);
        });
    },

    saveToCache: function(callback){
        if(this.licenses){
            fs.writeFile(this.cachedFile, JSON.stringify(this.licenses), callback);
        }
    },

    getLicenseTree: function(packageJSON, callback){
        var depth = 0;
        this.traverseStatus = {};

        for (var i = 0; i < packageJSON.list.length; i++) {
            this.traverseStatus[packageJSON.list[i].name] = 0;
        };

        this.iterateOverPackageJson(packageJSON, depth, 0, callback);
    },

    iterateOverPackageJson: function(packageJSON, depth, index, callback){
        if(packageJSON.list[index]){
            if(this._canDiscover(packageJSON.list[index].name, depth)){
                this.traversePackageDependencyTree(packageJSON.list[index], depth, function(){
                    return this.iterateOverPackageJson(packageJSON, depth, index + 1, callback);
                }.bind(this));
            }
            else{
                this.iterateOverPackageJson(packageJSON, depth, index + 1, callback);
            }
        }
        else{
            callback();
        }
    },

    traversePackageDependencyTree: function(singlePackage, depth, callback){
        var self = this;
        this.traverseStatus[singlePackage.name] = 1;
        console.log("Package:" + singlePackage.name);

        singlePackage.fillLicense(function(){
            console.log("License filled: " + singlePackage.license);
            singlePackage.discoverDependencies(function(packageJSON){
                if(self._hasDependencies(packageJSON)){
                    var newPackageJson = new PackageJSON(self.settings, self.reposParsed);
                    newPackageJson.parseFromCommonPackageJson(packageJSON, function(){
                        singlePackage.dependencies = newPackageJson.list;
                        var index = 0;

                        self.iterateOverInnerPackageJson(singlePackage.dependencies, depth+1, index, callback);
                    });
                }else{
                    callback();
                }
            });
        });
    },

    iterateOverInnerPackageJson: function(packageJSON, depth, index, callback){
        if(packageJSON[index]){
            if(this._canDiscover(packageJSON[index].name, depth)){
                this.traversePackageDependencyTree(packageJSON[index], depth, function(){
                    return this.iterateOverInnerPackageJson(packageJSON, depth, index + 1, callback);
                }.bind(this));
            }
            else{
                this.iterateOverInnerPackageJson(packageJSON, depth, index + 1, callback);
            }
        }
        else {
            callback();
        }
    },

    _canDiscover: function(name, depth){
        if(this.traverseStatus[name] === undefined){
            this.traverseStatus[name] = 0;
        }

        if(this.traverseStatus[name] === 0 && depth < this.settings.licenseDepth){
            return true;
        }

        return false;
    },

    _hasDependencies: function(packageJSON){
        return packageJSON && packageJSON['dependencies'] && !_.isEmpty(packageJSON.dependencies);
    }
}

module.exports = Licenses;
0
задан 21.01.2017, 04:36
0 ответов