m0603-jak 1.0.0 → 1.0.2
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.
- package/m0603-jak.js +3 -2
- package/package.json +1 -1
- package/index.js +0 -101
- package/info.txt +0 -31
- package/public/MailAPI.js +0 -26
- package/public/badRequest.html +0 -12
- package/public/favicon.ico +0 -0
- package/public/index.html +0 -21
package/m0603-jak.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const mailer = require('nodemailer')
|
|
2
2
|
const smtpTransport = require('nodemailer-smtp-transport');
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
function send(mailFrom, mailPassword, mailTo, message) {
|
|
5
5
|
let transporter = mailer.createTransport(smtpTransport({
|
|
6
6
|
host: 'smtp.gmail.com',
|
|
7
7
|
port: 587,
|
|
@@ -27,4 +27,5 @@ exports.send = (mailFrom, mailPassword, mailTo, message) => {
|
|
|
27
27
|
else
|
|
28
28
|
console.log(`Send from: ${mailFrom}, to: ${mailTo}, message: ${message}`);
|
|
29
29
|
})
|
|
30
|
-
}
|
|
30
|
+
}
|
|
31
|
+
module.exports=send;
|
package/package.json
CHANGED
package/index.js
DELETED
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
const http = require('http')
|
|
2
|
-
const fs = require('fs')
|
|
3
|
-
const path = require('path')
|
|
4
|
-
var url = require('url')
|
|
5
|
-
const {send} = require('./m0603-jak')
|
|
6
|
-
|
|
7
|
-
//#region Standard logic
|
|
8
|
-
const extToMeme = {
|
|
9
|
-
html: 'text/html',
|
|
10
|
-
css: 'text/css',
|
|
11
|
-
js: 'application/javascript',
|
|
12
|
-
gif: 'image/gif',
|
|
13
|
-
jpg: 'image/jpeg',
|
|
14
|
-
png: 'image/png',
|
|
15
|
-
svg: 'image/svg+xml',
|
|
16
|
-
txt: 'text/plain',
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
function sendFile(url, res) {
|
|
20
|
-
let filePath = path.join(__dirname, 'public', url)
|
|
21
|
-
const ext = path.extname(filePath)
|
|
22
|
-
if (!ext) {
|
|
23
|
-
filePath += '.html'
|
|
24
|
-
}
|
|
25
|
-
console.log(`req: ${filePath}`);
|
|
26
|
-
const contentType = extToMeme[ext.slice(1)] || 'text/html'
|
|
27
|
-
|
|
28
|
-
fs.readFile(filePath, (err, data) => {
|
|
29
|
-
if (err) {
|
|
30
|
-
fs.readFile("public/badRequest.html", (err, data) => {
|
|
31
|
-
if (err) {
|
|
32
|
-
throw err
|
|
33
|
-
}
|
|
34
|
-
res.writeHead(404, {
|
|
35
|
-
'Content-Type': 'text/html'
|
|
36
|
-
})
|
|
37
|
-
res.end(data)
|
|
38
|
-
})
|
|
39
|
-
return;
|
|
40
|
-
}
|
|
41
|
-
res.writeHead(200, {
|
|
42
|
-
'Content-Type': contentType
|
|
43
|
-
})
|
|
44
|
-
res.end(data)
|
|
45
|
-
})
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
function readBody(req) {
|
|
49
|
-
return new Promise((resolve) => {
|
|
50
|
-
const bodyData = [];
|
|
51
|
-
req.on("data", (chunk) => {
|
|
52
|
-
bodyData.push(chunk)
|
|
53
|
-
});
|
|
54
|
-
req.on("end", () => {
|
|
55
|
-
resolve(Buffer.concat(bodyData));
|
|
56
|
-
});
|
|
57
|
-
})
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
function readJSONBody(req) {
|
|
61
|
-
return new Promise((resolve) => {
|
|
62
|
-
readBody(req)
|
|
63
|
-
.then((data) => {
|
|
64
|
-
resolve(JSON.parse(data))
|
|
65
|
-
})
|
|
66
|
-
})
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
//#endregion
|
|
70
|
-
|
|
71
|
-
function processApi(req, res) {
|
|
72
|
-
if (/api\/mail/.test(req.url)) {
|
|
73
|
-
readJSONBody(req)
|
|
74
|
-
.then((body) => {
|
|
75
|
-
console.log(body);
|
|
76
|
-
send(body.from, body.password, body.to, body.message)
|
|
77
|
-
res.end()
|
|
78
|
-
})
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const server = http.createServer((req, res) => {
|
|
83
|
-
console.log(req.url)
|
|
84
|
-
let url = req.url;
|
|
85
|
-
switch (true) {
|
|
86
|
-
case /api/.test(url):
|
|
87
|
-
processApi(req, res)
|
|
88
|
-
break;
|
|
89
|
-
case "/" == url:
|
|
90
|
-
sendFile("index.html", res);
|
|
91
|
-
break;
|
|
92
|
-
default:
|
|
93
|
-
sendFile(url, res);
|
|
94
|
-
break;
|
|
95
|
-
}
|
|
96
|
-
})
|
|
97
|
-
|
|
98
|
-
const PORT = process.env.PORT || 3000
|
|
99
|
-
server.listen(PORT, () => {
|
|
100
|
-
console.log(`Server has been started on ${PORT}...`)
|
|
101
|
-
})
|
package/info.txt
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
1)Поясните назначение npm.
|
|
2
|
-
|
|
3
|
-
NPM – (расшифровывается как Node Package Manager) это менеджер пакетов (можно читать как “плагинов”)
|
|
4
|
-
для языка программирования JavaScript.
|
|
5
|
-
|
|
6
|
-
2)Перечислите команды npm, с помощью которых можно просмотреть список установленных пакетов.
|
|
7
|
-
|
|
8
|
-
npm list -g --depth=0
|
|
9
|
-
|
|
10
|
-
или npm list
|
|
11
|
-
|
|
12
|
-
3)Поясните назначение параметра –g в командах npm.
|
|
13
|
-
|
|
14
|
-
глобальные пакеты
|
|
15
|
-
|
|
16
|
-
4)Перечислите команды npm, с помощью которых можно скачать пакет.
|
|
17
|
-
|
|
18
|
-
npm istall
|
|
19
|
-
npm i
|
|
20
|
-
|
|
21
|
-
5)Поясните назначение файла package.json.
|
|
22
|
-
|
|
23
|
-
package.json: содержит метаданные проекта (название, версия, описание проекта, …),
|
|
24
|
-
список зависимостей вашего пакета, которые будут установлены при вызове команды npm install,
|
|
25
|
-
скрипты, вызывающие другие команды консоли.
|
|
26
|
-
|
|
27
|
-
6)Перечислите последовательность действий, позволяющих опубликовать пакет с помощью npm.
|
|
28
|
-
|
|
29
|
-
1-npm init -y
|
|
30
|
-
2-npm login
|
|
31
|
-
3-npm publish
|
package/public/MailAPI.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
let FromElement = document.getElementById("mail-from");
|
|
2
|
-
let PasswordElement = document.getElementById("mail-pass");
|
|
3
|
-
let ToElement = document.getElementById("mail-to");
|
|
4
|
-
let MessageElement = document.getElementById("message");
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
function SendMailMessage() {
|
|
8
|
-
let MailRequest = { from: FromElement.value, password: PasswordElement.value, to: ToElement.value, message: MessageElement.value }
|
|
9
|
-
fetch("api/mail",
|
|
10
|
-
{
|
|
11
|
-
method: 'POST',
|
|
12
|
-
headers: {
|
|
13
|
-
'Content-Type': 'application/json;charset=utf-8'
|
|
14
|
-
},
|
|
15
|
-
body: JSON.stringify(MailRequest)
|
|
16
|
-
})
|
|
17
|
-
.then((res) => {
|
|
18
|
-
if (res.status !== 200) {
|
|
19
|
-
console.log("err: " + res.status);
|
|
20
|
-
return;
|
|
21
|
-
}
|
|
22
|
-
return res.text();
|
|
23
|
-
}).then((data) => {
|
|
24
|
-
document.getElementById("InsertHere").innerHTML = data;
|
|
25
|
-
})
|
|
26
|
-
}
|
package/public/badRequest.html
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
<!DOCTYPE html>
|
|
2
|
-
<html lang="en">
|
|
3
|
-
<head>
|
|
4
|
-
<meta charset="UTF-8">
|
|
5
|
-
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
6
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
7
|
-
<title>Home</title>
|
|
8
|
-
</head>
|
|
9
|
-
<body>
|
|
10
|
-
<h1 style="text-align: center;">404 - Bad request</h1>
|
|
11
|
-
</body>
|
|
12
|
-
</html>
|
package/public/favicon.ico
DELETED
|
Binary file
|
package/public/index.html
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
<!DOCTYPE html>
|
|
2
|
-
<html lang="en">
|
|
3
|
-
<head>
|
|
4
|
-
<meta charset="UTF-8">
|
|
5
|
-
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
6
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
7
|
-
<title>Home</title>
|
|
8
|
-
</head>
|
|
9
|
-
<body>
|
|
10
|
-
<h1>ФИТ, 3 курс, 2 группа, Якушик Павел Геннадьевич</h1>
|
|
11
|
-
<!-- <form name="DBControl" onsubmit="processDBForm()" sub> -->
|
|
12
|
-
<label>mail from: </label><input id="mail-from" value=""></input><br/>
|
|
13
|
-
<label>mail password: </label><input id="mail-pass" type="password" value="xoaqveiizcuoztbx"></input><br/>
|
|
14
|
-
<label>mail to: </label><input id="mail-to" value=""></input><br/>
|
|
15
|
-
<label>message: </label><input id="message" value="Hello"></input><br/>
|
|
16
|
-
<button name="Send" type="submit" onclick="SendMailMessage()">Send</button>
|
|
17
|
-
<!-- </form> -->
|
|
18
|
-
<h2 id="InsertHere"></h2>
|
|
19
|
-
<script src="MailAPI.js"></script>
|
|
20
|
-
</body>
|
|
21
|
-
</html>
|