-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathindex.ts
42 lines (33 loc) · 1.69 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
import './bootstrap-globals';
import { createExpressHandler } from './createExpressHandler';
import express 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 /serverless/functions/app, which is used when the "npm run serverless:deploy" command is run.
const tokenFunction: ServerlessFunction = require('../serverless/functions/app/token').handler;
const tokenEndpoint = createExpressHandler(tokenFunction);
const turnCredentialsFunction: ServerlessFunction = require('../serverless/functions/app/turn-credentials').handler;
const turnCredentialsEndpoint = createExpressHandler(turnCredentialsFunction);
app.all('/app/token', tokenEndpoint);
app.all('/app/turn-credentials', turnCredentialsEndpoint);
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-diagnostics-react-app server running on ${PORT}`));