-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
75 lines (66 loc) · 1.71 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Load .env files
require("dotenv").config();
const express = require("express");
const bodyParser = require("body-parser");
const rateLimit = require("express-rate-limit");
const cors = require("cors");
const helmet = require("helmet");
const fs = require("fs");
const https = require("https");
const limiter = rateLimit({
windowMs: 1 * 60 * 1000, // 1 minute
max: 20, // limit each IP to 20 requests per windowMs
});
const app = express();
// apply to all requests
app.use(limiter);
app.use(
bodyParser.json({
limit: "1mb",
})
);
app.use(bodyParser.urlencoded({ limit: "1mb", extended: true }));
app.use(helmet());
app.use(cors());
// error handler
app.use(function (err, req, res, next) {
console.log(err.stack);
res.status(err.status || 500);
res.json({
errors: {
message: err.message,
error: err,
},
});
});
app.use("/api/", require("./controllers/emailToDidController"));
const port = 5004;
let key;
let cert;
try {
key = fs.readFileSync("/home/ubuntu/CERTS/server-key.pem");
cert = fs.readFileSync("/home/ubuntu/CERTS/server-cert.pem");
} catch (err) {
console.log("key or cert not available. Continuing... ");
}
if (key !== undefined && cert !== undefined) {
https
.createServer(
{
key: fs.readFileSync("/home/ubuntu/CERTS/server-key.pem"),
cert: fs.readFileSync("/home/ubuntu/CERTS/server-cert.pem"),
},
app
)
.listen(port, function () {
console.log(
"Email to did service listening on port 5004! Go to https://localhost:5004/"
);
});
} else {
const server = app.listen(process.env.PORT || port, function () {
console.log(
"Email to did service Listening on port " + server.address().port
);
});
}