-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp.ts
58 lines (47 loc) · 1.3 KB
/
http.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
import * as functions from 'firebase-functions';
import { db } from './init';
// Express
import * as express from 'express';
const cors = require('cors');
// Most basic HTTP Funtion
//https://us-central1-function-login-94e8c.cloudfunctions.net/basicHTTP?name=ajay
export const basicHTTP = functions.https.onRequest((request, response) => {
const name = request.query.name;
if (!name) {
response.status(400).send('ERROR you must supply a name :(');
}
response.send(`hello ${name}`);
});
// Custom Middleware
const auth = (req, res, next) => {
if (!req.headers.authorization) {
res
.status(400)
.send(
JSON.stringify({ res: 'unauthorized', authorization: req.headers })
);
}
next();
};
// Multi Route ExpressJS HTTP Function
const app = express();
app.use(cors({ origin: true }));
app.use(auth);
app.get('/cat', (request, response) => {
response.send('CAT');
});
app.get('/dog', (request, response) => {
response.send('DOG');
});
app.get('/products', async (req, res) => {
const docs = await db.collection('products').get();
res.send(
docs.docs.map((doc) => {
return {
productId: doc.id,
...doc.data(),
};
})
);
});
export const api = functions.https.onRequest(app);