如何返回此对象,而不是记录它?

时间:2022-10-04 07:21:14

I have a static site generator for which I want to list all available partials in the templates/partials folder. I need to format the list of partials as the following kind of object:

我有一个静态站点生成器,我想在其中列出templates / partials文件夹中的所有可用部分。我需要将partials列表格式化为以下类型的对象:

var partials = {
  'header': 'partials/header', // from './templates/partials/header.html'
  'footer': 'partials/footer' // from './templates/partials/footer.html'
};

I've followed this example to put together this node script:

我按照这个例子将这个节点脚本放在一起:

'use strict';

/**
 * Dependencies
 */

var fs = require("fs");
var path = require("path");
var p = 'templates/partials';

/**
 * Scan folder and return object with partials
 */

fs.readdir(p, function (err, files) {
  var obj = {};

  if (err) {
    throw err;
  };

  for (var i = 0; i < files.length; i++) {
    obj[path.basename(files[i], '.html')] = 'partials/' + files[i];
  };

  console.log(obj);
});

When executed from the root of the project it logs:

从项目的根目录执行时,它会记录:

{ footer: 'partials/footer.html',
  header: 'partials/header.html' }

Which is good, but I want to return it, not log it. Also I get the idea that what I've written is rather clunky. So my question is, how do I return the object, and how do I make this more robust?

哪个好,但我想退货,不记录。我也明白我写的东西是相当笨重的。所以我的问题是,如何返回对象,以及如何使其更加健壮?

2 个解决方案

#1


1  

You should provide a callback function:

你应该提供一个回调函数:

function readfiles(callback) {
  fs.readdir(p, function (err, files) {
    var obj = {};

    if (err) {
      throw err;
    };

    for (var i = 0; i < files.length; i++) {
      obj[path.basename(files[i], '.html')] = 'partials/' + files[i];
    };

    callback(obj);
  });
}

This would be used as follows:

这将使用如下:

readfiles(function(files) {
  // Do stuff with files here.
});

Callback functions allow IO to be done asynchronously; this often provides a large speedup to code because it does not need to wait for IO to finish. In this case, it is not so useful, but the callback pattern is very common in Node.js and so it's good practice to use it here to.

回调函数允许IO异步完成;这通常为代码提供了很大的加速,因为它不需要等待IO完成。在这种情况下,它不是那么有用,但回调模式在Node.js中很常见,因此在这里使用它是一种好习惯。

#2


1  

var results = fs.readdir(p, function(err, files) {
  var obj = {};

  if (err) {
    throw err;
  };

  for (var i = 0; i < files.length; i++) {
    obj[path.basename(files[i], '.html')] = 'partials/' + files[i];
  };

  // derectly deal with it here
  deal(obj);
});

or you can consider async:

或者您可以考虑异步:

async.waterfall([

    function(cb) {
      fs.readdir(p, cb)
    },
    function(files, cb) {
      for (var i = 0; i < files.length; i++) {
        obj[path.basename(files[i], '.html')] = 'partials/' + files[i];
      }
      cb(null, obj);
    }
  ],
  function(err, obj) {
    if (err) {
      throw err;
    }

    // do somethin else
  }
);

or you want sync return :

或者你想要同步返回:

var files = fs.readdirSync(p);
for (var i = 0; i < files.length; i++) {
  obj[path.basename(files[i], '.html')] = 'partials/' + files[i];
};

// here's your obj
obj;

read the document for more detail: http://nodejs.org/api/fs.html

阅读文档以获取更多详细信息:http://nodejs.org/api/fs.html

#1


1  

You should provide a callback function:

你应该提供一个回调函数:

function readfiles(callback) {
  fs.readdir(p, function (err, files) {
    var obj = {};

    if (err) {
      throw err;
    };

    for (var i = 0; i < files.length; i++) {
      obj[path.basename(files[i], '.html')] = 'partials/' + files[i];
    };

    callback(obj);
  });
}

This would be used as follows:

这将使用如下:

readfiles(function(files) {
  // Do stuff with files here.
});

Callback functions allow IO to be done asynchronously; this often provides a large speedup to code because it does not need to wait for IO to finish. In this case, it is not so useful, but the callback pattern is very common in Node.js and so it's good practice to use it here to.

回调函数允许IO异步完成;这通常为代码提供了很大的加速,因为它不需要等待IO完成。在这种情况下,它不是那么有用,但回调模式在Node.js中很常见,因此在这里使用它是一种好习惯。

#2


1  

var results = fs.readdir(p, function(err, files) {
  var obj = {};

  if (err) {
    throw err;
  };

  for (var i = 0; i < files.length; i++) {
    obj[path.basename(files[i], '.html')] = 'partials/' + files[i];
  };

  // derectly deal with it here
  deal(obj);
});

or you can consider async:

或者您可以考虑异步:

async.waterfall([

    function(cb) {
      fs.readdir(p, cb)
    },
    function(files, cb) {
      for (var i = 0; i < files.length; i++) {
        obj[path.basename(files[i], '.html')] = 'partials/' + files[i];
      }
      cb(null, obj);
    }
  ],
  function(err, obj) {
    if (err) {
      throw err;
    }

    // do somethin else
  }
);

or you want sync return :

或者你想要同步返回:

var files = fs.readdirSync(p);
for (var i = 0; i < files.length; i++) {
  obj[path.basename(files[i], '.html')] = 'partials/' + files[i];
};

// here's your obj
obj;

read the document for more detail: http://nodejs.org/api/fs.html

阅读文档以获取更多详细信息:http://nodejs.org/api/fs.html