This repository has been archived by the owner on Feb 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
56 lines (47 loc) · 1.56 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
// Hide the API keys.
require('dotenv').config();
var express = require('express');
var helper = require('sendgrid').mail;
var bodyParser = require("body-parser");
var app = express();
var PORT = process.env.PORT || 8888;
// parse application/x-www-form-urlencoded
app.use(bodyParser.json());
// parse application/json
app.use(bodyParser.json());
app.use(express.static(__dirname + '/public'));
app.listen(PORT, function () {
console.log('Listening to port ' + PORT);
});
// redirect CSS bootstrap
app.use('/css', express.static(__dirname + '/node_modules/bootstrap/dist/css'));
// load the single view file (angular will handle the page changes on the front-end)
app.get('*', (req, res) => {
res.sendfile('./public/index.html');
});
// SendGrid configuration
app.post('/sendEmail', (req, res) => {
var sg = require('sendgrid')(process.env.SENDGRID_API_KEY);
var from_email = new helper.Email('[email protected]');
var to_email = new helper.Email('[email protected]');
var subject = req.body.title;
var content = new helper.Content('text/plain', req.body.message);
var mail = new helper.Mail(from_email, subject, to_email, content);
var request = sg.emptyRequest({
method: 'POST',
path: '/v3/mail/send',
body: mail.toJSON(),
});
// Send mail
sg.API(request)
.then(response => {
console.log(response.statusCode);
console.log(response.body);
console.log(response.headers);
return res.status(response.statusCode).send();
})
.catch(error => {
//The full response is attached to error.response
console.log(error.response.statusCode);
});
});