Просмотр исходного кода

Started working on client/server communication

Brendan Abolivier 8 лет назад
Родитель
Сommit
41dbfb8bb2
Подписано: Brendan Abolivier <contact@brendanabolivier.com> Идентификатор ключа GPG: 8EF1500759F70623
4 измененных файлов: 94 добавлений и 6 удалений
  1. 63
    4
      front/form.js
  2. 2
    2
      front/index.html
  3. 1
    0
      package.json
  4. 28
    0
      server.js

+ 63
- 4
front/form.js Просмотреть файл

@@ -1,11 +1,49 @@
1
+var items = {
2
+    name: 'form_name',
3
+    addr: 'form_addr',
4
+    subj: 'form_subj',
5
+    text: 'form_text',
6
+};
7
+
8
+var server  = getServer();
9
+var xhrSend = new XMLHttpRequest();
10
+xhrSend.onreadystatechange = function() {
11
+    if (xhrSend.readyState == XMLHttpRequest.DONE) {
12
+        console.log(xhrSend);
13
+    }
14
+};
15
+
16
+
17
+// Returns the server's base URI based on the user's script tag
18
+// return: the SMAM server's base URI
19
+function getServer() {
20
+    var scripts = document.getElementsByTagName('script');
21
+    // Parsing all the <script> tags to find the URL to our file
22
+    for(var i = 0; i < scripts.length; i++) {
23
+        let script = scripts[i];
24
+        if(script.src) {
25
+            let url = script.src;
26
+            // This should be our script
27
+            if(url.match(/:[0-9]+\/form\.js$/)) {
28
+                // Port has been found
29
+                return url.match(/^(http:\/\/[^\/]+)/)[1];
30
+            }
31
+        }
32
+    }    
33
+}
34
+
35
+
36
+// Creates a form
37
+// id: HTML identifier of the document's block to create the form into
38
+// return: nothing
1 39
 function generateForm(id) {
2 40
     var el = document.getElementById(id);
3 41
     
4 42
     var input = {
5
-        name: getField('form_name', 'Your name', false, 'input'), // TODO: configurable prefix
6
-        addr: getField('form_addr', 'Your e-mail address', true, 'input'),
7
-        subj: getField('form_subj', 'Your message\'s subject', false, 'input'),
8
-        text: getField('form_text', 'Your message', false, 'textarea')
43
+        name: getField(items.name, 'Your name', false, 'input'), // TODO: configurable prefix
44
+        addr: getField(items.addr, 'Your e-mail address', true, 'input'),
45
+        subj: getField(items.subj, 'Your message\'s subject', false, 'input'),
46
+        text: getField(items.text, 'Your message', false, 'textarea')
9 47
     };
10 48
     
11 49
     // Adding nodes to document
@@ -100,4 +138,25 @@ function getSubmitButton(id, text) {
100 138
     submit.appendChild(button);
101 139
     
102 140
     return submit;
141
+}
142
+
143
+
144
+// Send form data through the XHR object
145
+// return: nothing
146
+function sendForm() {
147
+    xhrSend.open('POST', server + '/send');
148
+    xhrSend.setRequestHeader('Content-Type', 'application/json');
149
+    xhrSend.send(JSON.stringify(getFormData()));
150
+}
151
+
152
+
153
+// Fetch form inputs from HTML elements
154
+// return: an object containing all the user's input
155
+function getFormData() {
156
+    return {
157
+        name: document.getElementById(items.name + '_input').value,
158
+        addr: document.getElementById(items.addr + '_input').value,
159
+        subj: document.getElementById(items.subj + '_input').value,
160
+        text: document.getElementById(items.text + '_textarea').value
161
+    }
103 162
 }

+ 2
- 2
front/index.html Просмотреть файл

@@ -3,10 +3,10 @@
3 3
     <head>
4 4
         <meta charset="utf-8">
5 5
         <title>Send Me A Mail</title>
6
-        <script src="form.js" charset="utf-8"></script>
6
+        <script src="http://localhost:1970/form.js" charset="utf-8"></script>
7 7
     </head>
8 8
     <body>
9
-        <form id="smam"></form>
9
+        <div id="smam"></div>
10 10
         <script type="text/javascript">
11 11
             generateForm('smam');
12 12
         </script>

+ 1
- 0
package.json Просмотреть файл

@@ -15,6 +15,7 @@
15 15
     "start": "node --use_strict server.js"
16 16
   },
17 17
   "dependencies": {
18
+    "body-parser": "1.15.2",
18 19
     "express": "4.14.0",
19 20
     "node-minify": "1.3.9",
20 21
     "nodemailer": "2.4.2",

+ 28
- 0
server.js Просмотреть файл

@@ -0,0 +1,28 @@
1
+var bodyParser  = require('body-parser');
2
+var express     = require('express');
3
+var app = express();
4
+
5
+// Logging
6
+var printit = require('printit');
7
+var log = printit({
8
+    prefix: 'SMAM',
9
+    date: true
10
+});
11
+
12
+
13
+// Serve static (JS + HTML) files
14
+app.use(express.static('front'));
15
+app.use(bodyParser.urlencoded({ extended: true }));
16
+app.use(bodyParser.json());
17
+
18
+
19
+app.post('/send', function(req, res, next) {
20
+    res.header('Access-Control-Allow-Origin', '*');
21
+    res.status(200).send();
22
+    console.log(req.body);
23
+});
24
+
25
+
26
+app.listen(1970, function() {
27
+    log.info("Server started");
28
+});