-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.ts
31 lines (27 loc) · 891 Bytes
/
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
import {Request, Response} from 'express';
import directions from './src/directions';
import origins from './src/origins';
import places from './src/places';
/**
* Entry point into the Functions Framework.
* @see https://github.com/GoogleCloudPlatform/functions-framework-nodejs
*/
exports.function = (req: Request, res: Response) => {
const paths = {
'/directions': directions,
'/origins': origins,
'/places': places,
// Default route (at the end)
'/': () => res.send(Object.keys(paths)),
};
// Find the first route that matches
for (const [path, route] of Object.entries(paths)) {
if (req.path.startsWith(path)) {
return route(req, res);
}
}
// Allow CORS
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
res.send('No path found');
};