-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
53 lines (46 loc) · 1.66 KB
/
app.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
const express = require("express");
const bodyParser = require("body-parser");
module.exports = (mailer) => {
let app = express();
let requestId = -1;
app.use((req, res, next) => {
req.requestId = ++requestId;
console.log(`[request/${req.requestId}] ${new Date().toISOString()} ${req.method} ${req.path}`);
next();
});
app.all("/interested-person", (req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With,Content-Type");
next();
});
app.post("/interested-person", bodyParser.json(), (req, res) => {
let name = req.body.name;
let email = req.body.email;
if(!name || name == "") {
return res.status(400).send({
error: "Invalid or missing name " + name
});
}
if(!email || email == "" || email.indexOf("@") == -1) {
return res.status(400).send({
error: "Invalid or missing email " + email
});
}
mailer.send({
subject: "[opportunity] New interested person: " + name,
text: `${name}\n${email}`,
html: `<p>${name}<br>${email}</p>`
}, {
email: "[email protected]"
}, (error) => {
if(error) {
console.error("Failed to send opportunity email: " + name + " <" + email + ">", error);
return res.status(500).send({
error: "Failed to save request."
});
}
return res.status(200).send({ success: "OK" });
});
});
return app;
};