-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
60 lines (47 loc) · 1.51 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const express = require('express');
const path = require('path');
const fs = require('fs');
const http = require('http');
const bodyParser = require('body-parser');
// Объект с конфигами
const patternsObject = {};
// Путь к директории с конфигами
const dirPath = path.join(`${__dirname}/patterns/`);
// Считываем в конфигурационный файл объект вида
// String: String
const filenames = fs.readdirSync(dirPath);
filenames.forEach((item) => {
const filePath = dirPath + item;
const content = fs.readFileSync(filePath, 'utf8');
patternsObject[item] = content;
});
// port
const port = 3000;
// // Init app
const app = express();
const server = http.Server(app);
// Template engine -- EJS
app.set('view engine', 'ejs');
// Routing
app.get('/', (req, res) => {
res.render('index', { config: JSON.stringify(patternsObject) });
});
app.use(bodyParser.json());
// Принять запрос от клиента
app.post('/', (req, res) => {
const newDataJSON = JSON.stringify(req.body);
const newData = JSON.parse(newDataJSON);
const newDataLen = newData.length;
const ext = '.json';
for (let i = 0; i < newDataLen; i += 1) {
const fileName = newData[i].name;
const dataToWrite = JSON.stringify(newData[i]);
const fullPath = dirPath + fileName + ext;
fs.writeFileSync(fullPath, dataToWrite);
}
res.end();
});
// Serve static in root static folder
app.use(express.static('./static'));
// Port listening
server.listen(port);