2016-06-29 1 views
0

こんにちは私はパスを取り、ディレクトリ構造を解析する再帰関数を持つスクリプトを書いています。スクリプトは期待通りに動作しますが、それ自体で終了することはありません。スクリプトはJavascriptで書かれており、fs(ファイルシステム)やパスなどのNodeJsモジュールを使用します。Javascriptで書かれたスクリプトは単独で終了していませんか?

マイコード:

var parseDirectory = function(currPath, tags, func){ 
    var util = util || require('./util'); 
    var fs = fs || require('fs'); 
    var path = path || require('path'); 
    if(!util.fileExist(currPath)) { 
     throw new Error('Path is invalid, it does not exist.'); 
    } 

    //get the stat and do operation according to file type 
    var stat = fs.statSync(currPath); 

    //if directory 
    // - get all files inside the directory 
    // - push the currPath in tags 
    // - call parseDirectory for each file 
    // - pop from the tags array 
    if(stat.isDirectory()) { 
     var files = fs.readdirSync(currPath); 
     var fs = fs || require('fs'); 
     tags.push(path.parse(currPath).name); 
     files.forEach(function(file) { 
      parseDirectory(path.join(currPath,file),tags,func); 
     }); 
     tags.pop(); 
    } else if(stat.isFile()) { 
     func(currPath,tags); 
    } else { 
     throw new Error('Path is not a file or Directory'); 
    } 
} 

//connect to db 
var mongoose = mongoose || require('mongoose'); 
mongoose.connect('mongodb://localhost:27017/iconrepo'); 

// a sample call to parseDirectory 
parseDirectory('icon/png/',[],function(filePath, tags) { 
    var path = path || require('path'); 
    var fs = fs || require('fs'); 
    var File = require('./models/file'); 
    var stat = fs.statSync(filePath); 

    var file = new File({ 
     name : path.parse(filePath).name, 
     type : path.extname(filePath), 
     size : stat.size, 
     tags : tags, 
     path : filePath 
    }); 

    //console.log(file); 
    //file.save(function(err) { 
    // if(err) { 
    //  throw new Error(err); 
    // } 

    // console.log('Added ', filePath); 
    //}); 

    console.log('Exiting Callback for filePath',filePath); 
}); 

console.log('Last statement of the script'); 

File.jsは

// a file schema file 

var mongoose = require('mongoose'); 
var Schema = mongoose.Schema; 

var fileSchema = new Schema({ 
    name: String, 
    type: String, 
    size: {type: Number}, 
    tags: [String], 
    path: String 
}); 

module.exports = mongoose.model('File',fileSchema); 
// a file schema file 

var mongoose = require('mongoose'); 
var Schema = mongoose.Schema; 

var fileSchema = new Schema({ 
    name: String, 
    type: String, 
    size: {type: Number}, 
    tags: [String], 
    path: String 
}); 

module.exports = mongoose.model('File',fileSchema); 

は、誰もが私が作っています何の間違い私をポイントしてくださいことができます。

答えて

1

マングースがあなたのスクリプトを実行し続ける接続を確立しました。

mongoose.connection.close()あなたのスクリプトを終了してください。

+0

ありがとう@Kazenorinそれは働いた。 –

関連する問題