-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.ts
More file actions
60 lines (46 loc) · 1.39 KB
/
server.ts
File metadata and controls
60 lines (46 loc) · 1.39 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
import * as express from "express"
import * as path from "path"
import * as cors from "cors"
import helmet from "helmet"
import {db} from "./utils/db";
import {apiRouter} from "./routes/apiRoutes";
import {clientRouter} from "./routes/clientRoutes";
// Initialize ExpressJS App
const app = express()
// Configure
app.set("etag", false) // Disable etag-based caching
// PaaS proxies requests, required for express-rate-limit middleware
app.set("trust proxy", true)
app.use(cors())
app.use(helmet())
app.use(express.urlencoded({
extended: true
}))
app.use(express.json())
// Serve react build files (production build, `react-scripts build`)
// Always keep this on top, static files must be searched through first by the server
app.use(
express.static(
path.join(__dirname, 'client/build/')
)
)
// Use the API router here
app.use("/api/", apiRouter)
// Use the client router to serve client files
app.use(clientRouter)
// Serve app on production port
const appServer = app.listen(process.env.PORT || 8800, () => {
console.log("Communitty backend server is up and running in " + process.env.NODE_ENV + " mode!")
})
// Many IDEs / Ctrl-Cs send SIGINT to processes
process.on("SIGINT", () => {
appServer.close()
})
// Heroku sends SIGTERM to processes
process.on("SIGTERM", () => {
appServer.close()
})
appServer.on('close', async () => {
await db.end()
console.log("Stopping communitty backend server")
})