-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
43 lines (29 loc) · 948 Bytes
/
app.js
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
const express = require('express');
const app = express();
const { db, storage } = require('./firebase');
app.use(express.static('public'));
app.use(express.json());
app.set('trust proxy', true);
// STATIC
app.use(express.static('public'));
// INDEX
app.get('/', (_, res) => res.sendFile(__dirname + '/index.html'));
// SHORTS REDIRECT
app.get('/:id', async (req, res) => {
const { id } = req.params;
const shortQuerySnapshot = await db.shorts.where('id', '==', id).get();
if (shortQuerySnapshot.empty) {
return res.status(404).send('Short not found');
}
const short = shortQuerySnapshot.docs[0].data();
if (short.disabled) {
return res.status(403).send('Short is not currently available');
}
shortQuerySnapshot.docs[0].ref.update({
visited: db.FieldValue.increment(1),
});
res.redirect(short.url);
});
// API
app.use('/api/shorts', require('./api/shorts'));
app.listen(80, () => console.log('Listening on port 80'));