forked from omnistrate-oss/saasbuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
51 lines (47 loc) · 1.73 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
const next = require("next");
const express = require("express");
const path = require("path");
const { startMailServiceCron } = require("./src/server/mail-service/mail-cron");
const verifyEnvrionmentVariables = require("./src/server/utils/verifyEnvironmentVariables");
const dev = process.env.NODE_ENV !== "production";
const hostname = "localhost";
const port = dev ? 3000 : 8080;
// when using middleware `hostname` and `port` must be provided below
const app = next({ dev, hostname, port });
const handle = app.getRequestHandler();
const expressApp = express();
app.prepare().then(async () => {
//check if all required environement variables are available
const { isVerified, envVariablesStatus } = await verifyEnvrionmentVariables();
console.log("Environment variables status", envVariablesStatus);
if (isVerified) {
if (
process.env.NODE_INDEX === undefined ||
process.env.NODE_INDEX === 0 ||
process.env.NODE_INDEX === "0"
) {
//start the mail service cron job only if
//a)NODE_INDEX environment variable is not present
//b)NODE_INDEX environment variable === 0
startMailServiceCron();
}
}
expressApp.set("view engine", "ejs");
expressApp.set("views", path.join(__dirname, "src/server/views"));
expressApp.use(express.static(path.join(__dirname, "public")));
expressApp.use(async (request, response) => {
try {
if (!isVerified && process.env.NODE_ENV === "development") {
response.render("pages/setup-error", {
envVariablesStatus,
});
}
await handle(request, response);
} catch (error) {
response.render("pages/error");
}
});
expressApp.listen(port, () => {
console.log(`> Ready on http://${hostname}:${port}`);
});
});