Browse Source

添加首页 跟 根目录浏览功能

udbbbn 6 years ago
parent
commit
769b2ac1a3
5 changed files with 25 additions and 4 deletions
  1. 12 0
      index.html
  2. 2 0
      web.config
  3. 8 1
      webServer.js
  4. 3 3
      webServer_test.js
  5. 0 0
      www/3213/213.html

+ 12 - 0
index.html

@@ -0,0 +1,12 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <meta http-equiv="X-UA-Compatible" content="ie=edge">
+    <title>Document</title>
+</head>
+<body>
+    21321
+</body>
+</html>

+ 2 - 0
web.config

@@ -2,7 +2,9 @@
 <webconfig>
     <host>localhost</host>
     <port>8100</port>
+    <DirectoryIndex>index.html</DirectoryIndex>
     <resolveDir>
+        <dir>[/, false]</dir>
         <dir>[/www, false]</dir>
         <dir>[/111, false]</dir>
     </resolveDir>

+ 8 - 1
webServer.js

@@ -85,7 +85,7 @@ const passRouter = (routes, method, path) => (req, res) => {
             })
             req.params = params;
             it.fn(req, res);
-        } else if (it.method === 'get' && it.path.includes(req.url)) {
+        } else if (it.method === 'get' && it.path.includes(req.url) && it.path === '/') {
             // 若允许访问的目录的子目录未允许访问
             res.writeHead(403, {'content-type': 'text/plain;charset=utf-8'});
             res.end(`暂未有访问权限`);
@@ -147,6 +147,13 @@ app.dir = function (_dir, dirname, bool) {
                     app.dir(_dir + '/' + files[i], dirname, bool)
                 }
                 fileName = files[i];
+                // 根目录
+                if ((_dir === '/' || _dir === '') && !config['DirectoryIndex']) {
+                    _dir = '';
+                } else {
+                    handleStatic(res, config['DirectoryIndex'], config['DirectoryIndex'].split('.')[1])
+                    return
+                }
                 html += "<div><a  href='" +_dir + '/' + fileName + "'>" + fileName + "</a></div>";
             }
         } catch (e) {

+ 3 - 3
webServer_test.js

@@ -2,9 +2,9 @@
 var app = require('./webServer');
 
 // get请求路由
-app.get('/', function(req, res) {
-    res.end('HEllo World');
-})
+// app.get('/', function(req, res) {
+//     res.end('HEllo World');
+// })
 
 // post请求 ajax带参数
 app.post('/test', function(req, res) {

+ 0 - 0
www/3213/213.html