javascript - node.js callbacks http server read dir -


rookie problems, unable make script show file listings, think walk async, have nothing in errors help.

regards, marcelo

// ---- listfiles.js ----------------------------------------------------------- exports.walk = function(currentdirpath, extension,  callback) {      var fs = require('fs');     var regex = new regexp( extension + '$', 'g' );     fs.readdir( currentdirpath, function (err, files) {         if (err) callback(err);         files.filter(function(fname){             if(fname.match(regex)) {                 callback(null, fname);             }         })     });     console.log("fired callback."); } // walk('.', 'js', function(err, file){ console.log(file); }); runs ok  // ---- listfilestest.js -------------------------------------------------------- var http = require('http'); var content = require('./listfiles'); var args = process.argv.slice(2); console.log(args); // command line arguments (dir & extension)  http.createserver(function (req, res) {     var files = [];     res.writehead(200, {'content-type': 'text/plain'});     res.write('-- list of files --\n');     content.walk(args[0], args[1], function(err, data){         if(err) console.error(err);         //console.log(data);         res.write(data); // not printing dir listings     } );     res.end(); }).listen(9000); // nodemon listfilestest.js '.' 'js' 

you calling res.end() before of res.write() statements have executed. callback content.walk() asynchronous means happens indeterminate time in future.

you need call res.end() after res.write() statements done. way exports.walk() structured, there no way caller know when done listing files , calling callback have restructured indicate when done.


there many possible ways structure that. 1 simple way adding argument callback signals when list done.

// ---- listfiles.js ----------------------------------------------------------- exports.walk = function(currentdirpath, extension,  callback) {      var fs = require('fs');     var regex = new regexp( extension + '$', 'g' );     fs.readdir( currentdirpath, function (err, files) {         if (err) callback(err);         files.filter(function(fname){             if(fname.match(regex)) {                 // call callback filename, not done yet                 callback(null, false, fname);             }         })         // signal done listing files         callback(null, true);         console.log("done listing files.");     }); } // walk('.', 'js', function(err, done, file){ console.log(file); }); runs ok  // ---- listfilestest.js -------------------------------------------------------- var http = require('http'); var content = require('./listfiles'); var args = process.argv.slice(2); console.log(args); // command line arguments (dir & extension)  http.createserver(function (req, res) {     var files = [];     res.writehead(200, {'content-type': 'text/plain'});     res.write('-- list of files --\n');     content.walk(args[0], args[1], function(err, done, data){         if(err) {             console.error(err);             return;         }         if (done) {             res.end();         } else {             //console.log(data);             res.write(data); // not printing dir listings         }     }); }).listen(9000); 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -