-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
144 lines (120 loc) · 3.02 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
var express = require('express');
var app = express();
var bodyparser = require('body-parser');
var MongoClient = require('mongodb').MongoClient;
var db = null;
var ObjectID = require('mongodb').ObjectID;
var nodemailer = require('nodemailer');
const fs = require('fs');
var webshot = require('webshot');
var CronJob = require('cron').CronJob;
var cron = require('node-cron');
var async = require('async');
var request = require('request');
var gbk = require('gbk');
MongoClient.connect("mongodb://localhost:27017/arrDB",function(err,dbconn){
if(!err){
console.log("we are connected!");
db = dbconn;
}
});
app.use(express.static('public'));
app.use(bodyparser.json());
app.get('/projects',function(req,res,next){
db.collection('arr',function(err,arrCollection){
arrCollection.find().toArray(function(err,arr){
console.log(arr);
return res.json(arr);
})
});
//res.send(arr);
});
/*app.post('/projectsdata',function(req,res,next){
console.log(req.body);
db.collection('projects',function(err,projectsCollection){
var newResv=req.body.newResv;
projectsCollection.insert(newResv,{w:1},function(err){
return res.send(newResv);
return res.send();
});
});
});*/
app.get('/projectsdata',function(req,res,next){
db.collection('versiondt',function(err,versiondtCollection){
versiondtCollection.find().toArray(function(err,arr){
//console.log('finish reading cvs file data!');
return res.json(arr);
})
});
//res.send(arr);
});
/*creat pdf file*/
function getPdf(){
var options = {
screenSize: {
width: 1200,
height: 900
}
,shotSize:{
width: 900,
height:'all'
}
,renderDelay:10000
};
webshot('http://localhost:4005/','screenshot.pdf',options,function(err){
if(err){
console.log('something wrong');
}
});
}
getPdf();
/*send email*/
app.post('/subscribe',function(req,res,next){
//console.log(req.body.newSubs);
function sendMail(){
var transporter = nodemailer.createTransport({
service:'gmail',
auth:{
user:'[email protected]',
pass:'333testemailsender'
}
});
//setup email data
var mailOptions = {
from:'[email protected]',
//to:'[email protected]',
to:req.body.newSubs.receiver,
subject:['pdf file'],
text: 'send successfully!',
attachments:[{
path:'./screenshot.pdf',
contentType:"application/pdf"
}]
};
transporter.sendMail(mailOptions,function(err,info){
if(err){
console.log(err);
}else{
console.log('Email sent:' + info.response);
}
transporter.close();
});
}
//var rate = '00 00 9 * * 1-5'; //Daily: Monday-Friday 9:00 AM
var rate = '* * * * */1'; //every minute
/*if(req.body.newSubs.date){
console.log(req.body.newSubs.date);
var a = Date.parse(req.body.newSubs.date);
var b = new Date(a);
console.log(a);
rate = toString(a);
}*/
cron.schedule(rate,function(){
//getHTML();
sendMail();
})
res.send();
});
app.listen(4005, function(){
console.log('app listensing on port 4005!');
});