How to browse local files (the root directory contains multiple subdirectories)?
The core step
const http = require('http'); const path = require('path'); const url = require('url'); const fs = require('fs'); Const hostname = '127.0.0.1; const port = 8010; var documentRoot = 'F:/doc'; Var urls = []; const server = http.createServer((req,res)=>{ let pathname = url.parse(req.url).pathname; var urlname = req.url; var file = documentRoot + urlname; Console. log(" first pathName :" + pathName); // First pathName :/favicon.ico after clicking on the server directory (excluding /favicon.ico): first pathName :/server console.log(" urlName :" +) urlname); // urlName :/favicon.ico :/server console.log("file:" + file); / / file: D: / projects/node/lessons/servers/attached: file:D:/projects/node/lessons/servers/favicon.ico After click server directory (put/favicon. Ico excluded) : file: D: / projects/node/lessons/servers/server if (pathname = = '/') {fs. Readdir (documentRoot, Function (err, files) {// Read file directories asynchronously. Files2 is an array containing the names of all files in the specified directory. If (err){console.log("files err"); res.writeHeader(404,{ 'content-type' : 'text/html; charset="utf-8"' }); Res.write ('<h1> File read error </h1><p> The page you are looking for does not exist </p>'); res.end(); }else{ res.writeHeader(200,{ 'content-type' : 'text/html; charset="utf-8"' }); // urls=[]; for (let i = 0; i < files.length; i++) { let filename = files[i]; Fs.stat (filename,function(error,stats){if(error){res.write('<h1> analysis file error </h1><p> Page you are looking for does not exist </p>'); }else{// stats.isdirectory () determine if the given path is a directory type if(stats.isdirectory ()){urls.push(filename); res.write('<a href="'+ filename + '">'+ filename +'</a><br>'); }else{ res.write( files[i] +'<br>'); } if(i == files.length-1){ res.end(); }}}); }}}); }else if(pathname ! == "/favicon.ico" && pathname ! == "/"){//a link display //split() method to split a string into an array of strings, bounded by '/' to split the pathname string into an array of strings, [pathname.split("/").length-1] indicates that linkfilename takes only the last pathname. Let linkfilename = pathname.split("/")[pathname.split("/").length-1]; console.log("linkfilename:" + linkfilename); Linkfilename :asdasds let canRedirect = false; For (let j = 0; let j = 0; j < urls.length; J++) {// loop out the directory of files in urls. If the cut linkfilename equals the directory of files in the loop, then set the canRedirect to true so that the following can jump back to the next level and break and break out of the loop. If (linkfilename == urls[j]){canRedirect = true; break; }} if(canRedirect){// docRoot2 = documentRoot + pathName; Console. log(" Can jump "); let backurl = ""; for(let k=0; k < pathname.split("/").length-1; k++){ backurl += "/" + pathname.split("/")[k]; } console.log("backurl222" + backurl); Pathname.split ("/")[k] = null, backurl =1, backurl = null, pathname:/server, pathname:/server, pathname:/server, pathname:/server, pathname:/server, pathname:/server, pathname:/server, pathname. k < pathname.split("/").length-1; If (backurl == ""){backurl="/"; } console.log("pathname:" + pathname); Console. log("docRoot2:" + docRoot2); / / D: / projects/node/lessons/servers/server, that has entered the catalog page after click on the server console, log (" backUrl: "+ backUrl); // / res.writeHeader(200, {'content-type': 'text/html; charset="utf-8"'}); res.write('<a href="'+ backurl + '">.. </a><br>'); Fs. readdir(docRoot2, function (err, files2) { Files2 is an array containing the names of all files in the specified directory if(err){console.log("files err"); res.writeHeader(404,{ 'content-type' : 'text/html; charset="utf-8"' }); Res.write ('<h1> File read error </h1><p> The page you are looking for does not exist </p>'); res.end(); }else{ // urls=[]; Console. log(" subfile size: "+ files2.length); For (let m = 0; m < files2.length; m++) { let filename2 = files2[m]; let statfilename = docRoot2 +"/" + files2[m]; let linkpathname2 = pathname + "/" + files2[m]; console.log(statfilename); / / D after click server directory: / projects/node/lessons/servers/server/a.h HTML. TXT files2 [0] = a.h HTML. TXT console. The log (linkpathname2); Statfilename,function(error2,stats2){if(error2){if(error2){ Res.write ('<h1> Analysis file error </h1><p> The page you are looking for does not exist </p>'); }else{if (stats2.isdirectory ()) {// stats.isdirectory () determine if the given path is a directory type urls.push(filename2); res.write('<a href="' + linkpathname2 + '">' + filename2 + '</a><br>'); }else{res.write(files2[m] +'<br>');}else{res.write(files2[m] +'<br>'); } if(m == files2.length-1){ res.end(); }}}); }}}); }else{console.log(" error "); res.writeHeader(404,{ 'content-type' : 'text/html; charset="utf-8"' }); Res.write ('<h1>404 error </h1><p> The page you are looking for does not exist </p>'); res.end(); }}}); server.listen(port,hostname,()=>{ console.log(`Server running at http://${hostname}:${port}/`); })Copy the code