repl.it
@jajoosam/

jajoo.fun

Nodejs

No description

fork
loading
Files
  • index.js
  • Packager files
  • package-lock.json
  • package.json
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const express = require('express');
const airtable = require('airtable');
const request = require('request');
const rateLimit = require("express-rate-limit");

const app = express();

const limiter = rateLimit({
  windowMs: 15 * 60 * 1000, 
  max: 25
});

app.use(limiter);

airtable.configure({
    apiKey: process.env.KEY
})

const base = airtable.base(process.env.BASE);

const incrementViews = (id, count) => {
  base('redirect').update([
    {
      id,
      "fields": {
        "hits": count+1
      }
    }
  ]);
}

const search = (key, next) => {
  base('redirect').select({
        filterByFormula: `{key} = "${key}"`
  }).eachPage(records => {
      return next(records[0]);
  });
}

app.get(`/`, (req, res) => res.redirect(`/home`))

app.get(`/debug/:key`, (req, res)=> {
  search(req.params.key, record => {
    res.send(record);
  });
})

app.get(`/:key`, (req, res)=> {
  search(req.params.key, record => {
    if(!record){
      return res.send(`oof this not exist...????`)
    }
    if(!record.fields.files){
      res.redirect(record.fields.link);
    }
    else{
      let url = record.fields.files[0].url;
      if(record.fields.files[0].type.includes('image')){
        url = record.fields.files[0].thumbnails.large;
      }
      request.get(url).pipe(res);
    }
    incrementViews(record.id, record.fields.hits)
  });
})


app.listen(3000, () => {
  console.log('server started');
});
Fetching token
?