-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
245 lines (212 loc) · 5.38 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
var express = require('express');
var http = require('http');
var path = require('path');
var fs = require('fs');
var json2xls = require('json2xls');
var nodemailer = require('nodemailer');
var app = express();
var transporter = nodemailer.createTransport( {
host: "smtp.gmail.com",
secureConnection: true,
port: 587,
auth: {
user: '[email protected]',
pass: 'Dshtrih2'
}
});
app.engine('html', require('hogan-express'));
app.set('view options', {layout: true});
app.set('layout', 'layout');
app.enable('view cache');
app.set('port', process.env.PORT || 5000);
app.set('view engine', 'html');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'www')));
/**
*
* @type {string}
*/
var index = "../www/index";
app.get('/', function(req, res){
res.render(index)
});
/**
*
* @type {string}
*/
var getQuestion = "../www/question";
app.get('/question', function(req, res){
res.render(question, { "question": question })
});
/**
*
* @type {{}}
*/
var sendObj = {};
app.post('/send', function(req, res){
/**
*
* @type {{message: *, number: (*|data.number|number|res.fields.number|qt.number|Ht.number), contact: (*|Document.contact)}}
*/
sendObj = {
"message" : req.body.message,
"number" : req.body.number,
"contact" : req.body.contact
};
funcWriteJson(sendObj, res);
try{
funcSendEmail(sendObj)
} catch(e){
console.log(e)
}
res.writeHead(302, {
'Location': '/'
});
res.end();
});
/**
*
* @type {Array}
*/
var dataToWriteGlobal = [];
/**
*
* @type {string}
*/
var url = 'www/ok/data.json';
var readFile = fs.readFileSync( url );
var jsonObj = JSON.parse( readFile );
/**
*
* @type {Array}
*/
var arr = [];
for( var i = 0; i < jsonObj.length; i++){
arr.push(JSON.stringify(jsonObj[i]))
}
/**
*
* @param sendObj
* @param res
*/
var funcWriteJson = function(sendObj, res){
arr.push(JSON.stringify(sendObj));
/**
*
* @type {string}
*/
var dataToWrite = "[" + arr + "]";
fs.writeFileSync( url , dataToWrite );
dataToWriteGlobal = JSON.stringify(jsonObj);
dataToWriteNewFunc(dataToWrite);
funcWriteExcel(dataToWrite, res);
};
dataToWriteGlobal = JSON.stringify(jsonObj);
/**
*
* @param dataToWrite
* @param res
*/
var funcWriteExcel = function(dataToWrite, res){
var data = JSON.parse(dataToWrite);
/**
*
* @type {*|exports}
*/
var xls = json2xls(data);
try{
fs.writeFileSync('www/ok/data.xlsx', xls, 'binary');
dataToWriteGlobal = dataToWrite;
} catch (e){
console.log(e);
res.writeHead(302, {
'Location': '/'
});
res.end();
}
};
/**
*
* @param dataToWrite
* @returns {*}
*/
var dataToWriteNewFunc = function(dataToWrite){
return dataToWrite;
};
/**
*
* @type {string}
*/
var all = "../www/all";
app.get('/24', function(req, res){
res.render(all, { "data": dataToWriteGlobal })
});
/**
*
* @param sendObj
*/
var funcSendEmail = function(sendObj) {
var htmlMgs =
"<hr>Повідомлення - " + sendObj.message +
"<hr>№ справи або рішення - " + sendObj.number +
"<hr>Контакти - " + sendObj.contact +
'<hr><b><a href="http://judgelustration.herokuapp.com/24">переглнути таблицю judgelustration</a></b>' +
'<hr><b><a href="http://judgelustration.herokuapp.com/">перейти на головну judgelustration</a></b>' +
'<hr><b><a href="http://judgelustration.herokuapp.com/ok/data.xlsx">скачати EXCEL judgelustration</a></b>';
var mailOptions = {
from: 'judgelustration ✔ <judgelustration.com>',
subject: 'judgelustration',
text: 'judgelustration',
html: htmlMgs
};
transporter.sendMail(mailOptions, function(error, info){
if(error){
console.log(error);
}else{
console.log('Message sent: ' + info.response + mailOptions.html);
}
});
};
//var sitemap = require('./server/xml/xml');
//sitemap.xml(app)
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
//app.configure(function (){
// app.use(express.cookieParser('keyboard cat'));
// app.use(express.session({ cookie: { maxAge: 60000 }}));
//});
//if ('development' == app.get('env')) {
// app.use(express.errorHandler());
// mongoose.connect('mongodb://localhost/clothing');
//}
//var MySignIn = new mongoose.Schema({
// name : String,
// pass : String
//});
//var user = mongoose.model('users',MySignIn);
//
//app.post('/register', function(req, res){
// new user({
// name : req.body.name,
// pass : req.body.pass
// }).save(function(err,doc){
// if(err) res.json(err);
// else res.statusCode = 302;
// res.setHeader("Location", "/users");
// res.end();
// });
//});
//
//var admin = "../www/admin/admin";
//app.get('/admin', function(req, res){
// res.render(admin)
// mongoose.model('users').find(function(err, users){
// res.send(users);
// });
//});