-
Notifications
You must be signed in to change notification settings - Fork 727
/
index.ts
48 lines (38 loc) · 2.07 KB
/
index.ts
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
import './bootstrap-globals';
import { createExpressHandler } from './createExpressHandler';
import express, { RequestHandler } from 'express';
import path from 'path';
import { ServerlessFunction } from './types';
const PORT = process.env.PORT ?? 8081;
const app = express();
app.use(express.json());
// This server reuses the serverless endpoints from the "plugin-rtc" Twilio CLI Plugin, which is used when the "npm run deploy:twilio-cli" command is run.
// The documentation for this endpoint can be found in the README file here: https://github.com/twilio-labs/plugin-rtc
const tokenFunction: ServerlessFunction = require('@twilio-labs/plugin-rtc/src/serverless/functions/token').handler;
const tokenEndpoint = createExpressHandler(tokenFunction);
const recordingRulesFunction: ServerlessFunction = require('@twilio-labs/plugin-rtc/src/serverless/functions/recordingrules')
.handler;
const recordingRulesEndpoint = createExpressHandler(recordingRulesFunction);
const noopMiddleware: RequestHandler = (_, __, next) => next();
const authMiddleware =
process.env.REACT_APP_SET_AUTH === 'firebase' ? require('./firebaseAuthMiddleware') : noopMiddleware;
app.all('/token', authMiddleware, tokenEndpoint);
app.all('/recordingrules', authMiddleware, recordingRulesEndpoint);
app.use((req, res, next) => {
// Here we add Cache-Control headers in accordance with the create-react-app best practices.
// See: https://create-react-app.dev/docs/production-build/#static-file-caching
if (req.path === '/' || req.path === 'index.html') {
res.set('Cache-Control', 'no-cache');
res.sendFile(path.join(__dirname, '../build/index.html'), { etag: false, lastModified: false });
} else {
res.set('Cache-Control', 'max-age=31536000');
next();
}
});
app.use(express.static(path.join(__dirname, '../build')));
app.get('*', (_, res) => {
// Don't cache index.html
res.set('Cache-Control', 'no-cache');
res.sendFile(path.join(__dirname, '../build/index.html'), { etag: false, lastModified: false });
});
app.listen(PORT, () => console.log(`twilio-video-app-react server running on ${PORT}`));