Nodejs fs.js TyperError,适用于mac但不适用于windows

时间:2017-05-04 00:27:36

标签: node.js fs

我收到此错误:

fs.js:81 throw new TyperError('Expected options to be either an object or a string. )

它不会在我的Mac上执行,但它会在Windows上执行。

我不确定如何解决这个问题,我不太了解nodejs

这里是我的全部代码,不确定问题出在哪里,因为我在几个地方使用fs

// load modules
var request = require('request');

// create text file on disk
var filename = Date.now(); // date for filename
var fs = require('fs'); // code to make file
fs.writeFile("" + filename + ".csv", "", function(err) {
    if (err) {
        return console.log(err);
    }

    console.log("The file " + filename + ".csv was created!");
}); // create text file on disk end

// read urls file
var fs  = require("fs");
fs.readFile("urls.txt", function(err, f){
    var urls = f.toString().split('\n');
    console.log("Finished reading URLs file...")
    // use the array

// loop through every file
for (var i = 0; i < urls.length; i++) {
        console.log('\x1b[32m%s\x1b[0m',"Loaded to memory -> ",urls[i]);

setTimeout(main, i * 300, urls[i], i, urls.length);

}// end loop

}); // read urls end

console.log("Loop done...");

    function main (url, lineNumber, totalLines) {
        // get request to url
        request(url, function (error, response, body) {
            if (typeof response == 'undefined' || response == null) {
            return;
            }
            if (response.statusCode !== 200) { return; }
    //      console.log('error:', error); // Print the error if one occurred 
          console.log('\x1b[2m%s\x1b[0m','StatusCode for ' + url + ' ->', response && response.statusCode); // Print the response status code if a response was received
    //      console.log('body:', body); // Print the HTML for the Google homepage. 

            //define my regex
            var myregex = /href="([^"]*contact[^"]*)"/gi;

            // make sure it's not null
            if (typeof body == 'undefined' || body == null || body.match(myregex) == 'undefined' || body.match(myregex) == null){
            return;
            }

            // find contact urls using regex and puts it in array
            results = body.match(myregex).slice(1);
     //       console.log(results);

                //loop through arrays of regexes
                for (var x = 0; x < results.length; x++) { 
                    //remove href from string
                    results[x] = results[x].replace('href="', '');
                    results[x] = results[x].replace('"', '');



                    //add root domain if it doesn't have it
                    if (results[x].match(/http/) == null) {
                    //trim to root
                    trim = RegExp(/(http:\/\/|https:\/\/).+?\//);
                    console.log("Has no root -> " + url);
                    url = url.match(trim)[0]; 
                    console.log("Trimming domain... Done -> " + url);
                    //add domain
                    results[x] = url + results[x];
                    //clean double slashes
                    results[x] = results[x].replace(/([^:])(\/\/+)/g, '$1/');
                    console.log("Adding root complete -> " + results[x]);

                    }
                    else {
                        console.log("Found contact url for " + url + " -> " + results[x]);
                    }

                    // writing to disk
                    var fs = require('fs');
                    fs.appendFileSync(filename + ".csv",lineNumber + "," + results[x] + "\n", function(err) {
                        if (err) {
                            return console.log(err);
                        }
                    }); // writing to disk end
                console.log('\x1b[32m%s\x1b[0m', "Added to the text file " + filename + ".csv " + results[x] + " (" + lineNumber + " of " + totalLines + " " + Math.floor((lineNumber / totalLines) * 100) + "% completed!)");
                }

        }); // end get request to url
    }; //end main

1 个答案:

答案 0 :(得分:1)

appendFileSync不接受回调作为参数。将其替换为appendFile。参考:https://nodejs.org/api/fs.html#fs_fs_appendfile_file_data_options_callback