从NodeJS的文件夹中读取所有文件名

时间:2019-05-17 10:11:40

标签: node.js sitemap

在添加路由对象以从Angular文件夹中获取所需文件的值之前,我一直试图从其工作的文件夹中读取文件名 但是在添加了路由部分之后,我无法使代码运行walker.on方法

var router = require( "express" ).Router();
var walk = require( 'walk' );
var HashMap = require( 'hashmap' );
var extract = require( 'extract-zip' );
var map = new HashMap();
module.exports = function ( arrg ) {
    router.post( arrg.config.apiUrlInitial + "/selectedCountry", ( req, res, next ) => {
        var input_Id_from_Angular  = req.body.country;
        console.log( 'inside selectedCountry.js', input_Id_from_Angular );
        var file_path;
        var walker = walk.walk( '../../category_xml/', { followLinks: true } );
        console.log('walker' , walker.on)
        // Reading all the files with in the folder
        walker.on( 'file', function ( root, file, next ) {
            console.log('**********************');
            company_id = Object.values( ( file.name ).split( '_' ) )[ 2 ]; //extracting company id only 
            map.set( company_id, root + '/' + file.name )
            next();
        } );

        // returning the xml-file from zip file with matching company id coming from angular
        walker.on( 'end', function () {
            console.log( 'file walked' );
            file_path = map.get( input_Id_from_Angular );
            extract( file_path,
                { dir: 'D:/Projects/Node/public/' }, function ( err ) {
                    if ( err ) console.log( 'err -----> ', err.stack )
                } );
        } );
    } );
    return router;
}

预期是我从给定ID从zip文件中提取文件到特定文件夹,该文件与哈希图中具有该文件夹中显示的文件的键值的名称匹配。

0 个答案:

没有答案