forked from harrypunia/TCR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
130 lines (101 loc) · 3.31 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
const express = require('express'),
app = express(),
bodyParser = require('body-parser'),
moment = require('moment'),
mysql = require('mysql'),
cors = require('cors'),
nodemailer = require('nodemailer');
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
// Database connection.
var con = mysql.createConnection({
host : 'catrescue.ccuxgnxok5zx.us-east-1.rds.amazonaws.com',
user : 'root',
password : 'Password1234',
port : 3306,
database : 'catrescue'
});
app.use(express.static('public'));
app.post('/Shelter/addCat', (request, response) => {
// Handles submitting a new cat.
const catObj = request.body;
var intakeTime = moment.valueOf();
var name = catObj.catName;
var breed = catObj.breed;
var sql = "INSERT INTO Cat (intakeDate, name, primaryColor, weight, fivTested, furcpDate, age, breed, sex, vaccinesUpToDate, spayNeut, behaviour, medHist, comments) VALUES ('2018-05-12', '"+name+"', 'Brown', 5, false, '2019-05-23', 5, '"+breed+"', 'Male', false, true, 'Nothing1', 'Nothing2', 'Nothing3')";
// Execute command.
con.query(sql, function (err, result) {
if (err) throw err;
console.log("Successfully inserted into DB.");
// Send email.
sendEmail();
});
// Close database.
con.end();
// Redirect.
response.sendFile(__dirname + '/public/TCR/index.html');
});
app.get('/api/allcats', (req,res)=>{
const selectAll = `SELECT * FROM Cat`;
con.query(selectAll, (err, result)=>{
if (err) throw err;
console.log(result);
res.json(result);
});
});
app.get('/', (req, res) => {
console.log(__dirname+"/public/Shelter/index.html");
res.sendFile(__dirname + '/public/Shelter/index.html');
});
app.post('/', (req, res) => {
res.sendFile(__dirname + '/public/Shelter/index.html');
});
function sendEmail() {
var transporter = nodemailer.createTransport({
service: 'outlook.com',
secureConnection: false, // TLS requires secureConnection to be false
port: 587, // port for secure SMTP
auth: {
user: '[email protected]',
pass: 'RdUA@doAzoPiTUsAqD@K2Xi*s$r9T4'
}
})
var mailOptions = {
from: '[email protected]',
to: '[email protected]',
subject: 'New Cats in Shelter',
html: '<p>A new kitten arrived at the South Street Shelter. <bold>PLEASE</bold></p>'
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
};
// Not found middleware
app.use((req, res, next) => {
return next({status: 404, message: 'not found'})
});
// Error Handling middleware.
app.use((err, req, res, next) => {
let errCode, errMessage;
if (err.errors) {
// mongoose validation error
errCode = 400; // bad request
const keys = Object.keys(err.errors);
// report the first validation error
errMessage = err.errors[keys[0]].message;
} else {
// generic or custom error
errCode = err.status || 500;
errMessage = err.message || 'Internal Server Error';
}
res.status(errCode).type('txt')
.send(errMessage);
});
// listen for requests.
var listener = app.listen(3000, function() {
console.log('Your app is listening on port ' + listener.address().port);
});