@gonjy_monjy/m06_vda 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/app.js +36 -0
  2. package/index.html +66 -0
  3. package/m06_vda.js +31 -0
  4. package/package.json +26 -0
package/app.js ADDED
@@ -0,0 +1,36 @@
1
+ const http = require('http');
2
+ const url = require('url');
3
+ const fs = require('fs');
4
+ const send = require('./m06_vda').send;
5
+
6
+ const port = 3000;
7
+ const hostname = 'localhost';
8
+ const appPassword = '<appPassword>';
9
+
10
+ const server = http.createServer(async (req, res) => {
11
+ res.statusCode = 200;
12
+ res.setHeader('Conent-Type', 'text/html');
13
+ res.setHeader('charset', 'utf-8');
14
+
15
+ const page = url.parse(req.url, true);
16
+
17
+
18
+ if (page.pathname === '/') {
19
+ res.setHeader('Content-Type', 'text/html');
20
+ res.end(fs.readFileSync('index.html'));
21
+ } else if (page.pathname === '/send')
22
+ {
23
+ let body = '';
24
+ req.on('data', (data) => {
25
+ body += data;
26
+ });
27
+ req.on('end', () => {
28
+ let options = JSON.parse(body);
29
+ res.end(send(options.from, options.to, appPassword, options.text));
30
+ })
31
+ }
32
+ });
33
+
34
+ server.listen(port, hostname, () => {
35
+ console.log(`Server running at http://${hostname}:${port}/`);
36
+ });
package/index.html ADDED
@@ -0,0 +1,66 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Lab5 - Mailer</title>
7
+ <style>
8
+ fieldset {
9
+ width: 400px;
10
+ margin: 100px auto;
11
+ display: block;
12
+ }
13
+
14
+ legend {
15
+ font-size: 24px;
16
+ }
17
+
18
+ label {
19
+ font-size: 20px;
20
+ }
21
+
22
+ input {
23
+ display: block;
24
+ margin-bottom: 20px;
25
+ }
26
+
27
+ textarea {
28
+ margin-bottom: 20px;
29
+ }
30
+ </style>
31
+ </head>
32
+ <body>
33
+ <fieldset>
34
+ <legend>Заполните форму</legend>
35
+ <form action="index.html">
36
+ <label for="input_from">FROM</label>
37
+ <input type="text" name="from" id="input_from">
38
+ <label for="input_to">TO</label>
39
+ <input type="text" name="to" id="input_to">
40
+ <label for="input_text">TEXT</label><br />
41
+ <textarea type="text" name="text" id="input_text"></textarea><br />
42
+ <input type="submit" name="submit" id="submit" value="Отправить запрос">
43
+ </form>
44
+ </fieldset>
45
+
46
+ <script>
47
+ document.querySelector("#submit").addEventListener("click", async (event) => {
48
+ event.preventDefault();
49
+ let from = document.querySelector("#input_from");
50
+ let to = document.querySelector("#input_to");
51
+ let text = document.querySelector("#input_text");
52
+ let bodyText = `{
53
+ "from": "${from.value.toString()}",
54
+ "to": "${to.value.toString()}",
55
+ "text": "${text.value.toString()}"
56
+ }`;
57
+ console.log(bodyText);
58
+
59
+ fetch("http://localhost:3000/send", {
60
+ method: "POST",
61
+ body: bodyText
62
+ });
63
+ });
64
+ </script>
65
+ </body>
66
+ </html>
package/m06_vda.js ADDED
@@ -0,0 +1,31 @@
1
+ const nodemailer = require("nodemailer");
2
+
3
+ const send = (fromAddr, toAddr, mailPass, message) => {
4
+ const options = {
5
+ from: fromAddr,
6
+ to: toAddr,
7
+ subject: "Lab05 task #3",
8
+ text: message,
9
+ };
10
+ const transporter = nodemailer.createTransport({
11
+ host: 'smtp.gmail.com',
12
+ port: 465,
13
+ secure: false,
14
+ service: "Gmail",
15
+ auth: {
16
+ user: fromAddr,
17
+ pass: mailPass,
18
+ },
19
+ });
20
+
21
+
22
+ transporter.sendMail(options, (err, info) => {
23
+ if (err) {
24
+ console.log(err);
25
+ } else {
26
+ console.log(info);
27
+ }
28
+ });
29
+ }
30
+
31
+ module.exports.send = send;
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "@gonjy_monjy/m06_vda",
3
+ "version": "1.0.0",
4
+ "description": "Lab6",
5
+ "main": "app.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1",
8
+ "dev": "nodemon app.js"
9
+ },
10
+ "devDependencies": {
11
+ "nodemon": "^2.0.20"
12
+ },
13
+ "author": "Volikov Dmitry",
14
+ "license": "ISC",
15
+ "dependencies": {
16
+ "nodemailer": "^6.9.7"
17
+ },
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+ssh://git@github.com/0ero-1ne/lab5-nodejs.git"
21
+ },
22
+ "bugs": {
23
+ "url": "https://github.com/0ero-1ne/lab5-nodejs/issues"
24
+ },
25
+ "homepage": "https://github.com/0ero-1ne/lab5-nodejs#readme"
26
+ }