简单的文件夹浏览器源代码

let fs = require("fs");

let http = require("http");

let path = require("path");

let server = http.createServer((request, response) => {

    let url = request.url

    //过滤掉浏览器标签页的标签

    if (url == "/favicon.ico") {

        response.end("")

        return

    }

    console.log(url)

    let dir = "test"

    //这一步是关键

    if (url !== "/") {

        dir = "." + url

    }

    let fileNames = fs.readdirSync(dir);

    let htmlTag = ""

    fileNames.forEach(file => {

        let filepath = path.join(dir, file);

        let status = fs.statSync(filepath);

        if (status.isDirectory()) {

            htmlTag += "" + file + ""

        } else {

            htmlTag += "" + file + ""

        }

    })

    response.end(makeHtml(htmlTag))

})

server.listen(8000)

function makeHtml(html) {

    return `

   

       

        Title

       

              integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

   

   

       

           

            ${html}

           

       

   

`

}

你可能感兴趣的:(简单的文件夹浏览器源代码)