-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (45 loc) · 1.35 KB
/
index.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
44
45
46
47
48
49
50
51
require('dotenv').config();
const express = require('express');
const app = express();
const port = process.env.PORT;
const Airtable = require('airtable');
Airtable.configure({
endpointUrl: 'https://api.airtable.com',
apiKey: process.env.AIRTABLE_APIKEY,
});
const base = Airtable.base('appVGDKZEwAVKVttl');
let links = {};
base('Links')
.select({
// Selecting the first 3 records in Grid view:
view: 'Grid view',
})
.eachPage(
function page(records, fetchNextPage) {
// This function (`page`) will get called for each page of records.
records.forEach(function(record) {
links[record.get('Slug')] = record.get('Destination');
});
// To fetch the next page of records, call `fetchNextPage`.
// If there are more records, `page` will get called again.
// If there are no more records, `done` will get called.
fetchNextPage();
},
function done(err) {
if (err) {
console.error(err);
return;
}
},
);
app.get('/', (req, res) => res.redirect(301, process.env.DEFAULT_REDIRECT));
app.get('/:slug', (req, res) => {
if (links[req.params.slug]) {
res.redirect(301, links[req.params.slug]);
} else {
res.redirect(301, res.redirect(301, process.env.DEFAULT_REDIRECT));
}
});
app.listen(port, () =>
console.log(`App listening on port ${process.env.PORT}!`),
);