1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| const http = require('http') const server = http.createServer() const fs = require('fs') server.on('request', (req, res) => { let url = req.url let wwwDir = 'F:/nodejs/Node_test/resource' fs.readFile('./resource/index.html', (err, data) => { if (err) { return res.end('404 Not Foun.') } fs.readdir(wwwDir, (err, files) => { if (err) { return res.end('文件不存在.') }
let content = ''; files.forEach((item) => { content += ` <tr> <td data-value="nodejs资料(7天)/"><a class="icon dir" href="/F:/nodejs/Node_test/nodejs%E8%B5%84%E6%96%99%EF%BC%887%E5%A4%A9%EF%BC%89/">${item}</a> </td> <td class="detailsColumn" data-value="0"></td> <td class="detailsColumn" data-value="1596634470">2020/8/5 下午9:34:30</td> </tr>` });
data = data.toString(); data = data.replace('***', content);
res.end(data); }) }) }); server.listen(3000, () => { console.log('服务器启动成功,可以通过 http://127.0.0.1:3000/ 来访问'); })
JAVASCRIPT
|