-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
62 lines (54 loc) · 1.67 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { Request, Response } from "express";
import express from 'express';
import dotenv from 'dotenv';
import cors from "cors";
import path from "path";
import AirtableService from "./server/service/AirtableService";
import { airtableConfigs } from "./server/airtableConfig";
dotenv.config();
const app: express.Application = express();
app.use(cors({
allowedHeaders: [
'Origin',
'X-Requested-With',
'Content-Type',
'Accept',
'X-Access-Token',
],
credentials: true,
methods: 'GET,HEAD,OPTIONS,PUT,PATCH,POST,DELETE',
preflightContinue: false,
}));
//express only serves static assets in productions
if (process.env.NODE_ENV === "production") {
app.use(express.static(path.join(__dirname,'client', 'build')));
}
//api calls
app.get('/api/health', (req: Request, res: Response) => res.send("ok"));
for (const config of airtableConfigs) {
const airtable = new AirtableService(config);
// fetch whole table
app.get(`/api/${config.route}`, async (req: Request, res: Response) => {
try {
const r = await airtable.getCachedTableContent();
return res.send(r);
} catch(e) {
return res.status(500).send(e);
}
});
// find individual recordv
app.get(`/api/${config.route}/:id`, async (req: Request, res: Response) => {
try {
const r = await airtable.getCachedRecord(req.params.id);
return res.send(r);
} catch(e) {
return res.status(500).send(e);
}
});
}
app.get('/*', function (req, res) {
res.sendFile(path.join(__dirname, 'client', 'build', 'index.html'));
});
app.listen(process.env.PORT, () => {
console.log(`[server]: Server is running at https://localhost:${process.env.PORT}`, `${new Date()}`);
});