Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
66 lines (54 loc) · 1.75 KB
/
Copy pathapp.js
File metadata and controls
66 lines (54 loc) · 1.75 KB
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
//dotenv config
import "dotenv/config"
import express from "express"
import morgan from "morgan"
import router from "./src/services/routing/routes.js"
import { verifySMTP } from "./src/utils/smtp.verify.js"
import { verifyAuth } from "./src/utils/verify.origin.js"
import contentNegotiator from "./src/utils/content.negotiator.js"
import cors from "cors"
import limiter from "./src/utils/rate.limit.js"
import { logger } from "./src/utils/logger.config.js"
const app = express()
// Morgan config
app.use(morgan("dev", {
stream: { write: (message) => logger.info(message.trim()) }
}))
app.set("trust proxy", 1)
// Cors
const websites = process.env.WEBSITES || ""
const sitesArray = websites.split(",").map(site => site.trim())
app.use(cors(
{
methods: ["GET", "POST", "OPTIONS"],
origin: sitesArray,
allowedHeaders: ["Content-Type", "Authorization", "authv1"],
credentials: true
}
))
app.use(limiter)
// Enforce json only content-type
app.use(contentNegotiator)
// Verify origin on every request
app.use(async (req, res, next) => {
const isValid = await verifyAuth(req, res)
if (isValid) {
next()
}
})
app.use(express.json())
// Server config and startup
const PORT = process.env.NODE_PORT || 5000
const HOSTNAME = "0.0.0.0"
app.listen(PORT, HOSTNAME, () => {
(async () => {
try {
await verifySMTP() // Verify SMTP connection on startup.
logger.info("☑️ SMTP Verified");
logger.info(`☑️ Server started at http://${HOSTNAME}:${PORT}`)
app.use("/api", router)
} catch (error) {
logger.error("🚫 Error occurred while starting the server", error)
}
})()
})