Javascript 如何在nodejs中存储两个文件处理程序?

Javascript 如何在nodejs中存储两个文件处理程序?,javascript,node.js,Javascript,Node.js,我需要打开一个文件,解析它,发出http请求并将数据保存到另一个文件中。我能在不将所有代码嵌套在一个地方的情况下完成吗?(这会降低代码的可读性。) 我希望最后能有这样的东西。是否可以写入全局范围 on_write_file = function(err, write_file) { // parse the data from the opening file }; on_read_file = function(err, read_file) { fs.open('end_fi

我需要打开一个文件,解析它,发出http请求并将数据保存到另一个文件中。我能在不将所有代码嵌套在一个地方的情况下完成吗?(这会降低代码的可读性。)

我希望最后能有这样的东西。是否可以写入全局范围

on_write_file = function(err, write_file) {
    // parse the data from the opening file
};
on_read_file = function(err, read_file) {
    fs.open('end_file', 'w', on_write_file);
};

fs.open('start_file', 'r', on_read_file);

相反,在web服务器中,使用globals是危险的,那么我该怎么办?

是的,您可以使用
.bind
将数据转换为函数

on_write_file = function(read_file, err, write_file) {
    // parse the data from the opening file
};
on_read_file = function(err, read_file) {
    fs.open('end_file', 'w', on_write_file.bind(null, read_file));
};

fs.open('start_file', 'r', on_read_file);