-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
147 lines (133 loc) · 3.55 KB
/
Copy pathapp.js
File metadata and controls
147 lines (133 loc) · 3.55 KB
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
const express = require('express');
const bodyParser = require('body-parser');
const http = require('http');
const path = require('path');
const app = express();
const keys = require('./config/keys');
const nodemailer = require('nodemailer');
const Connection = require('tedious').Connection;
const Request = require('tedious').Request;
const cors = require('cors');
app.use(express.static(path.join(__dirname, 'build')));
// Parsers
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());
// Front End
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'build/index.html'));
});
// Email
app.post('/contact', (req, res) => {
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: keys.email,
pass: keys.pass
}
});
const mailOptions = {
from: 'contact.haave@gmail.com',
to: '', // enter recipient
subject: 'Contact Page',
html: `<p>Name: ${req.body.name}</p>
<p>Company: ${req.body.company}</p>
<p>Email: ${req.body.email}</p>
<p>Phone: ${req.body.phone}</p>
<p>Message: ${req.body.message}</p>`
};
transporter.sendMail(mailOptions, (err, info) => {
if (err) {
console.log(err);
res.sendStatus(500);
} else {
console.log(info);
res.sendStatus(200);
}
});
});
/* ===================== SQL ===================== */
app.get('/db', (req, res) => {
const config = {
userName: keys.sqlUser,
password: keys.sqlPass,
server: 'haavedbserver.database.windows.net',
options: {
database: 'haavedb',
encrypt: true
}
};
const connection = new Connection(config);
// Attempt to connect and execute queries if connection goes through
connection.on('connect', function(err) {
if (err) {
console.log(err);
} else {
queryDatabase();
}
});
function queryDatabase() {
let output = [];
// Read all rows from table
request = new Request('SELECT * FROM dbo.Articles', function(
err,
rowCount,
rows
) {
// Delete duplicate db entries
var unique = [];
output.filter(item => {
const i = unique.findIndex(x => x.title == item.title);
if (i === -1) {
unique.push(item);
}
});
res.send(unique);
// process.exit();
});
request.on('row', function(columns) {
columns.forEach(function(column) {
const val = String(column.value);
if (
// val.indexOf('uploaded a file: <') > -1 &&
val.indexOf('Image:') > -1 &&
val.indexOf('Article Title:') > -1 &&
val.indexOf('Article URL:') > -1 &&
val.indexOf('Publication Date:') > -1
) {
output.push(parseSlackText(val));
}
});
});
connection.execSql(request);
}
});
/* ============= End SQL =========== */
/* =========== Parse Slack Text ========= */
function parseSlackText(strRaw) {
// var strImg = strRaw.split('uploaded a file: <')[1];
// strImg = strImg.split('> and commented:')[0];
// strImg = strImg.split('|')[0];
var strImg = strRaw.split('Image: <')[1];
strImg = strImg.split('>')[0];
var strTitle = strRaw.split('Article Title:')[1];
strTitle = strTitle.split('Article URL:')[0];
strTitle = strTitle.trim();
var strURL = strRaw.split('Article URL: <')[1];
strURL = strURL.split('>')[0];
var strDate = strRaw.split('Publication Date:')[1];
strDate = strDate.trim();
var strResult = {
title: strTitle,
img: strImg,
url: strURL,
pubDate: strDate
};
return strResult;
}
/* ============== End Parse =========== */
//Set Port
const port = process.env.PORT || '8080';
app.set('port', port);
const server = http.createServer(app);
server.listen(port, () => console.log(`Running on localhost:${port}`));