Browse Source

提交测试示例

udbbbn 6 years ago
parent
commit
5ca009ee0e
8 changed files with 146 additions and 14 deletions
  1. 1 1
      .vscode/launch.json
  2. 0 0
      111/123.png
  3. 28 0
      package-lock.json
  4. 4 1
      package.json
  5. 1 1
      web.config
  6. 28 11
      webServer.js
  7. 28 0
      webServer_test.js
  8. 56 0
      www/test.html

+ 1 - 1
.vscode/launch.json

@@ -8,7 +8,7 @@
         "type": "node",
         "type": "node",
         "request": "launch",
         "request": "launch",
         "name": "Launch Program",
         "name": "Launch Program",
-        "program": "${workspaceFolder}/app.js"
+        "program": "${workspaceFolder}/webServer_test.js"
     },
     },
         {
         {
             "type": "node",
             "type": "node",

+ 0 - 0
111/123.png


+ 28 - 0
package-lock.json

@@ -138,6 +138,12 @@
       "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz",
       "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz",
       "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac="
       "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac="
     },
     },
+    "crypto": {
+      "version": "1.0.1",
+      "resolved": "https://registry.npmjs.org/crypto/-/crypto-1.0.1.tgz",
+      "integrity": "sha512-VxBKmeNcqQdiUQUW2Tzq0t377b54N2bMtXO/qiLa+6eRRmmC4qT3D4OnTGoT/U6O9aklQ/jTwbOtRMTTY8G0Ig==",
+      "dev": true
+    },
     "debug": {
     "debug": {
       "version": "2.6.9",
       "version": "2.6.9",
       "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
       "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
@@ -595,6 +601,12 @@
       "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.1.tgz",
       "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.1.tgz",
       "integrity": "sha512-kKvNJn6Mm93gAczWVJg7wH+wGYWNrDHdWvpUmHyEsgCtIwwo3bqPtV4tR5tuPaUhTOo/kvhVwd8XwwOllGYkbg=="
       "integrity": "sha512-kKvNJn6Mm93gAczWVJg7wH+wGYWNrDHdWvpUmHyEsgCtIwwo3bqPtV4tR5tuPaUhTOo/kvhVwd8XwwOllGYkbg=="
     },
     },
+    "sax": {
+      "version": "1.2.4",
+      "resolved": "https://registry.npmjs.org/sax/-/sax-1.2.4.tgz",
+      "integrity": "sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw==",
+      "dev": true
+    },
     "semver": {
     "semver": {
       "version": "5.5.0",
       "version": "5.5.0",
       "resolved": "https://registry.npmjs.org/semver/-/semver-5.5.0.tgz",
       "resolved": "https://registry.npmjs.org/semver/-/semver-5.5.0.tgz",
@@ -990,6 +1002,22 @@
       "resolved": "https://registry.npmjs.org/wtf-8/-/wtf-8-1.0.0.tgz",
       "resolved": "https://registry.npmjs.org/wtf-8/-/wtf-8-1.0.0.tgz",
       "integrity": "sha1-OS2LotDxw00e4tYw8V0O+2jhBIo="
       "integrity": "sha1-OS2LotDxw00e4tYw8V0O+2jhBIo="
     },
     },
+    "xml2js": {
+      "version": "0.4.19",
+      "resolved": "https://registry.npmjs.org/xml2js/-/xml2js-0.4.19.tgz",
+      "integrity": "sha512-esZnJZJOiJR9wWKMyuvSE1y6Dq5LCuJanqhxslH2bxM6duahNZ+HMpCLhBQGZkbX6xRf8x1Y2eJlgt2q3qo49Q==",
+      "dev": true,
+      "requires": {
+        "sax": "1.2.4",
+        "xmlbuilder": "9.0.7"
+      }
+    },
+    "xmlbuilder": {
+      "version": "9.0.7",
+      "resolved": "https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-9.0.7.tgz",
+      "integrity": "sha1-Ey7mPS7FVlxVfiD0wi35rKaGsQ0=",
+      "dev": true
+    },
     "xmlhttprequest-ssl": {
     "xmlhttprequest-ssl": {
       "version": "1.5.5",
       "version": "1.5.5",
       "resolved": "https://registry.npmjs.org/xmlhttprequest-ssl/-/xmlhttprequest-ssl-1.5.5.tgz",
       "resolved": "https://registry.npmjs.org/xmlhttprequest-ssl/-/xmlhttprequest-ssl-1.5.5.tgz",

+ 4 - 1
package.json

@@ -11,7 +11,10 @@
     "socketio": "^1.0.0",
     "socketio": "^1.0.0",
     "underscore": "^1.9.0"
     "underscore": "^1.9.0"
   },
   },
-  "devDependencies": {},
+  "devDependencies": {
+    "crypto": "^1.0.1",
+    "xml2js": "^0.4.19"
+  },
   "scripts": {
   "scripts": {
     "test": "echo \"Error: no test specified\" && exit 1"
     "test": "echo \"Error: no test specified\" && exit 1"
   },
   },

+ 1 - 1
web.config

@@ -4,6 +4,6 @@
     <port>8100</port>
     <port>8100</port>
     <resolveDir>
     <resolveDir>
         <dir>[/www, false]</dir>
         <dir>[/www, false]</dir>
-        <dir>[/www/156/123, false]</dir>
+        <dir>[/111, false]</dir>
     </resolveDir>
     </resolveDir>
 </webconfig>
 </webconfig>

+ 28 - 11
webServer.js

@@ -54,8 +54,22 @@ const passRouter = (routes, method, path) => (req, res) => {
             // 匹配到中间件
             // 匹配到中间件
             it.fn(req, res, next);
             it.fn(req, res, next);
         } else if ((it.method === method || it.method === 'all') && (it.path === path || it.path === "*")) {
         } else if ((it.method === method || it.method === 'all') && (it.path === path || it.path === "*")) {
-            // 匹配到路由
-            it.fn(req, res);
+            if (it.method === "get") {
+                // 获取query数据 获取get请求参数
+                req.query = url.parse(req.url, true).query;
+                // 匹配到路由
+                it.fn(req, res);
+            } else if (it.method === "post") {
+                // 获取body数据 获取post请求参数
+                let info = '';
+                req.addListener('data', (chunk) => {
+                    info += chunk
+                }).addListener('end', () => {
+                    req.body = JSON.parse(info);
+                    // 匹配到路由
+                    it.fn(req, res);
+                })
+            }
         } else if (it.path.includes(':') && (it.method === method || it.method === 'all') && (replaceParams(it.path).test(path))) {
         } else if (it.path.includes(':') && (it.method === method || it.method === 'all') && (replaceParams(it.path).test(path))) {
             // 模式匹配
             // 模式匹配
             let index = 0;
             let index = 0;
@@ -183,7 +197,16 @@ app.resolve = () => {
 
 
 // 目录配置字符串解析并调用dir函数
 // 目录配置字符串解析并调用dir函数
 app.doDir = () => {
 app.doDir = () => {
-    for(let i of config.resolveDir.dir) {
+    if (Array.isArray(config.resolveDir.dir)) {
+        for(let i of config.resolveDir.dir) {
+            if (i.indexOf("[") === 0) {
+                i = i.substring(1, i.length - 1);
+            }
+            i = i.split(',');
+            app.dir(i[0], __dirname, i[1]);
+        }
+    } else {
+        let i = config.resolveDir.dir;
         if (i.indexOf("[") === 0) {
         if (i.indexOf("[") === 0) {
             i = i.substring(1, i.length - 1);
             i = i.substring(1, i.length - 1);
         }
         }
@@ -193,12 +216,6 @@ app.doDir = () => {
     app.listen(config.port, config.host);
     app.listen(config.port, config.host);
 }
 }
 
 
-app.use('/blog', (req, res, next) => {
-    console.log('%s %s', req.method, req.url);
-    next()
-})
-app.get('/blog/:id', (req, res, next) => {
-    res.end('hello ' + req.params.id)
-})
-
 app.resolve();
 app.resolve();
+
+module.exports = app

+ 28 - 0
webServer_test.js

@@ -0,0 +1,28 @@
+// 导入模块
+var app = require('./webServer');
+
+// get请求路由
+app.get('/', function(req, res) {
+    res.end('HEllo World');
+})
+
+// post请求 ajax带参数
+app.post('/test', function(req, res) {
+    res.end(JSON.stringify(req.body))
+})
+
+// get请求 ajax带参数
+app.get('/test', function(req, res) {
+    res.end(JSON.stringify(req.query))
+})
+
+// use 中间件
+app.use('/blog', (req, res, next) => {
+    console.log('%s %s', req.method, req.url);
+    next()
+})
+
+// 模式匹配
+app.get('/blog/:id', (req, res, next) => {
+    res.end('hello ' + req.params.id)
+})

+ 56 - 0
www/test.html

@@ -0,0 +1,56 @@
+<!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>
+    <h3>这是个测试文件</h3>
+</body>
+</html>
+<script>
+    function post() {
+        var xmlhttp;
+            if(window.XMLHttpRequest) {
+                xmlhttp = new XMLHttpRequest();
+            }else if(window.ActiveXObject) {
+                xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
+            }
+            //回调
+            xmlhttp.onreadystatechange = function(){
+                if(xmlhttp.readyState == 4) {
+                    if(xmlhttp.status == 304 || (xmlhttp.status >= 200 && xmlhttp.status < 300)) {
+                        var renderMessage = JSON.parse(xmlhttp.responseText);
+                        console.log(renderMessage);
+                    }
+                }
+            }.bind(this);
+            //请求
+            xmlhttp.open('post', '/test', true);
+            xmlhttp.setRequestHeader('Content-Type','application/json;charset=utf-8');
+            xmlhttp.send(JSON.stringify({id:'12321', params: 'qwe'}));
+    }
+    function get() {
+        var xmlhttp;
+            if(window.XMLHttpRequest) {
+                xmlhttp = new XMLHttpRequest();
+            }else if(window.ActiveXObject) {
+                xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
+            }
+            //回调
+            xmlhttp.onreadystatechange = function(){
+                if(xmlhttp.readyState == 4) {
+                    if(xmlhttp.status == 304 || (xmlhttp.status >= 200 && xmlhttp.status < 300)) {
+                        var renderMessage = JSON.parse(xmlhttp.responseText);
+                        console.log(renderMessage);
+                    }
+                }
+            }.bind(this);
+            //请求
+            xmlhttp.open('get', '/test?name=213&sex=1', true);
+            xmlhttp.setRequestHeader('Content-Type','application/json;charset=utf-8');
+            xmlhttp.send();
+    }
+</script>