-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
83 lines (70 loc) · 1.88 KB
/
index.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
const express = require("express");
const cors = require("cors");
const path = require("path");
const ejs = require("ejs");
const SGmail = require("@sendgrid/mail");
const port = process.env.PORT || 4000;
const app = express();
//to enable cors
app.use(cors());
//set ecpress view engine
app.set("view engine", "ejs");
let SendGrid_Key = "YOUR_SG_API_KEY_HERE";
SGmail.setApiKey(SendGrid_Key);
//routes which handles the requests
app.get("/hello", (req, res, next) => {
let emailTemplate;
let capitalizedFirstName = "John";
let userEmail = "[email protected]";
ejs
.renderFile(path.join(__dirname, "views/welcome-mail.ejs"), {
user_firstname: capitalizedFirstName,
confirm_link: "http://www.8link.in/confirm=" + userEmail
})
.then(result => {
emailTemplate = result;
const message = {
to: userEmail,
from: { email: "[email protected]", name: "8link" },
subject: "Welcome link",
html: emailTemplate
};
return SGmail.send(message)
.then(sent => {
// Awesome Logic to check if mail was sent
res.status(200).json({
message: "Welcome mail was sent"
});
})
.catch(err => {
console.log("Error sending mail", err);
res.status(400).json({
message: "Welcome mail was not sent",
error: err
});
});
//res.send(emailTemplate);
})
.catch(err => {
res.status(400).json({
message: "Error Rendering emailTemplate",
error: err
});
});
});
app.use((req, res, next) => {
const error = new Error("Not Found");
error.status = 404;
next(error);
});
app.use((error, req, res, next) => {
res.status(error.status || 500);
res.json({
error: {
message: error.message
}
});
});
app.listen(port, () => {
console.log(`listening on port ${port}..`);
});