This is the sixth day of my participation in the August More text Challenge. For details, see: August More Text Challenge

The readdir command is used to obtain all file names in the specified path

File structure

The implementation code

const fs = require('fs');

fs.readdir('G:/pink_code/Node_Study/02'.(err,list) = > {
    if(! err) {console.log(list); }})Copy the code

Code output

[ '01_http-helloWorld.js'.'02_ use readdir to get all file names.js'.'www' ]
Copy the code

Use the template engine in your browser

1. The installation art – the template

npm install art-template
Copy the code

2. Introduce the Art-template through the script tag

<script src="./node_modules/art-template/lib/template-web.js"></script>
Copy the code

3. Invoke using template engine syntax

<script src="./node_modules/art-template/lib/template-web.js"></script>
<script type='text/template' id = 'tpl'>This is a{{name}}
</script>

<script>
    const test = template('tpl', {name: 'China'
    });
    console.log(test);
</script>
Copy the code

Use the template engine in Node

1. The installation art – the template

npm install art-template
Copy the code

2. Load the Art-template to the module that needs to use the template engine

3. Check the documentation and use the TEMPLATE engine API

A small example of using a template engine in Node

const template = require('art-template');

const test = template.render('hello {{name}}', {name: 'NodeJs'
})

console.log(test);
Copy the code

The output

hello NodeJs
Copy the code

A small example of using a template engine to render HTML

HTML structure

<html dir="ltr" lang="zh">

<head>
    <meta charset="utf-8">
    <meta name="google" value="notranslate">

    <style>
        h1 {
            border-bottom: 1px solid #c0c0c0;
            margin-bottom: 10px;
            padding-bottom: 10px;
            white-space: nowrap;
        }

        table {
            border-collapse: collapse;
        }

        th {
            cursor: pointer;
        }

        td.detailsColumn {
            -webkit-padding-start: 2em;
            text-align: end;
            white-space: nowrap;
        }

        a.icon {
            -webkit-padding-start: 1.5 em;
            text-decoration: none;
            user-select: auto;
        }

        a.icon:hover {
            text-decoration: underline;
        }

        a.file {
            background: url("data:image/png; base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAABnRSTlMAAAAAAABupgeRAAABHUlEQVR42o2RMW7DIBiF3498iHRJD5JKHurL+CRVB p+i2T16tTynF2gO0KSb5ZrBBl4HHDBuK/WXACH4eO9/CAAAbdvijzLGNE1TVZXfZuHg6XCAQESAZXbOKaXO57eiKG6ft9PrKQIkCQqFoIiQFBGlFIB5nvM8t 9aOX2Nd18oDzjnPgCDpn/BH4zh2XZdlWVmWiUK4IgCBoFMUz9eP6zRN75cLgEQhcmTQIbl72O0f9865qLAAsURAAgKBJKEtgLXWvyjLuFsThCSstb8rBCaAQ hDYWgIZ7myM+TUBjDHrHlZcbMYYk34cN0YSLcgS+wL0fe9TXDMbY33fR2AYBvyQ8L0Gk8MwREBrTfKe4TpTzwhArXWi8HI84h/1DfwI5mhxJamFAAAAAElFT kSuQmCC ") left top no-repeat;
        }

        a.dir {
            background: url("data:image/png; base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAd5JREFUeNqMU79rF UEQ/vbuodFEEkzAImBpkUabFP4ldpaJhZXYm/RiZWsv/hkWFglBUyTIgyAIIfgIRjHv3r39MePM7N3LcbxAFvZ2b2bn22/mm3XMjF+HL3YW7q28YSIw8mBKo BihhhgCsoORot9d3/ywg3YowMXwNde/PzGnk2vn6PitrT+/PGeNaecg4+qNY3D43vy16A5wDDd4Aqg/ngmrjl/GoN0U5V1QquHQG3q+TPDVhVwyBffcmQGJm SVfyZk7R3SngI4JKfwDJ2+05zIg8gbiereTZRHhJ5KCMOwDFLjhoBTn2g0ghagfKeIYJDPFyibJVBtTREwq60SpYvh5++PpwatHsxSm9QRLSQpEVSd7/TYJU b49TX7gztpjjEffnoVw66+Ytovs14Yp7HaKmUXeX9rKUoMoLNW3srqI5fWn8JejrVkK0QcrkFLOgS39yoKUQe292WJ1guUHG8K2o8K00oO1BTvXoW4yasclU TgZYJY9aFNfAThX5CZRmczAV52oAPoupHhWRIUUAOoyUIlYVaAa/VbLbyiZUiyFbjQFNwiZQSGl4IDy9sO5Wrty0QLKhdZPxmgGcDo8ejn+c/6eiK9poz15K w7Dr/vN/z6W7q++091/AQYA5mZ8GYJ9K0AAAAAASUVORK5CYII= ") left top no-repeat;
        }

        a.up {
            background: url("data:image/png; base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAmlJREFUeNpsU0toU 0EUPfPysx/tTxuDH9SCWhUDooIbd7oRUUTMouqi2iIoCO6lceHWhegy4EJFinWjrlQUpVm0IIoFpVDEIthm0dpikpf3ZuZ6Z94nrXhhMjM3c8895977BBHB2 PznK8WPtDgyWH5q77cPH8PpdXuhpQT4ifR9u5sfJb1bmw6VivahATDrxcRZ2njfoaMv+2j7mLDn93MPiNRMvGbL18L9IpF8h9/TN+EYkMffSiOXJ5+hkD+Pd qcLpICWHOHc2CC+LEyA/K+cKQMnlQHJX8wqYG3MAJy88Wa4OLDvEqAEOpJd0LxHIMdHBziowSwVlF8D6QaicK01krw/JynwcKoEwZczewroTvZirlKJs5CqQ 5CG8pb57FnJUA0LYCXMX5fibd+p8LWDDemcPZbzQyjvH+Ki1TlIciElA7ghwLKV4kRZstt2sANWRjYTAGzuP2hXZFpJ/GsxgGJ0ox1aoFWsDXyyxqCs26+yd magFN/rRjymJ1898bzGzmQE0HCZpmk5A0RFIv8Pn0WYPsiu6t/Rsj6PauVTwffTSzGAGZhUG2F06hEc9ibS7OPMNp6ErYFlKavo7MkhmTqCxZ/jwzGA9Hx82 H2BZSw1NTN9Gx8ycHkajU/7M+jInsDC7DiaEmo1bNl1AMr9ASFgqVu9MCTIzoGUimXVAnnaN0PdBBDCCYbEtMk6wkpQwIG0sn0PQIUF4GsTwLSIFKNqF6DVr Qq+IWVrQDxAYQC/1SsYOI4pOxKZrfifiUSbDUisif7XlpGIPufXd/uvdvZm760M0no1FZcnrzUdjw7au3vu/BVgAFLXeuTxhTXVAAAAAElFTkSuQmCC ") left top no-repeat;
        }

        html[dir=rtl] a {
            background-position-x: right;
        }

        #parentDirLinkBox {
            margin-bottom: 10px;
            padding-bottom: 10px;
        }

        #listingParsingErrorBox {
            border: 1px solid black;
            background: #fae691;
            padding: 10px;
            display: none;
        }
    </style>

    <title id="title">C:\Users\HP\Desktop\ share file \ index</title>

</head>

<body>

    <div id="listingParsingErrorBox">Oh, no! Google Chrome can't read the data sent by the server. please<a
            href="http://code.google.com/p/chromium/issues/entry">Report an error</a>, and send it<a href="LOCATION">The original list</a>.</div>

    <h1 id="header">C:\Users\HP\Desktop\ share file \ index</h1>

    <div id="parentDirLinkBox" style="display: block;">
        <a id="parentDirLink" class="icon up" href="/C:/Users/HP/Desktop/%E5%85%B1%E4%BA%AB%E6%96%87%E4%BB%B6/..">
            <span id="parentDirText">[Parent directory]</span>
        </a>
    </div>

    <table>
        <thead>
            <tr class="header" id="theader">
                <th id="nameColumnHeader" tabindex="0" role="button">The name of the</th>
                <th id="sizeColumnHeader" class="detailsColumn" tabindex="0" role="button">The size of the</th>
                <th id="dateColumnHeader" class="detailsColumn" tabindex="0" role="button">Modification date</th>
            </tr>
        </thead>
        <tbody id="tbody">
            {{each files}} 
            <tr>
                <td data-value="{{$value}}"><a class="icon file" draggable="true" href="{{$value}}">{{$value}}</a>
                </td>
                <td class="detailsColumn" data-value="193955">189 kB</td>
                <td class="detailsColumn" data-value="1627464963">On the afternoon of 2021/7/28 5:36:03</td>
            </tr>
            {{/each}}
        </tbody>
    </table>

</body>

</html>
Copy the code

The Node code

const http = require('http');
const template = require('art-template');
const fs = require('fs');

const server = http.createServer();

server.on('request'.(req, res) = > {
    const url = req.url;

    // File path
    const filePath = 'G:/pink_code/Node_Study/02';
    // Obtain all file names in the file path
    let listName;
    fs.readdir(filePath, (err, list) = > {
        if (!err) {
            listName = list;
        }
    })
    // Read the template file
    fs.readFile('./www/template.html'.(err, data) = > {
        if(! err) { data = data.toString(); test = template.render(data,{files: listName
            })
            res.end(test);
        } else {
            console.log('Error reading file', err); }}); })// Listen to port 3000
server.listen(3000.(err) = > {
    if(! err) {console.log('Server started successfully! '); }})Copy the code

Implementation effect