repl.it
Nodejs

spongebob as a service!

fork
loading
Files
  • index.js
  • index.html
  • 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
const fastify = require(`fastify`)()
const fs = require('fs')
const axios = require('axios')

fastify.get(`/`, (request, reply) => {
  reply.type(`text/html`).send(fs.createReadStream('index.html'));
})

fastify.get(`/spongebob.gif`, (request, reply) => {
  axios.get(`https://api.giphy.com/v1/gifs/random?api_key=${process.env.GIPHY}&tag=spongebob&rating=g`)
    .then(function(response) {
      const data = response.data.data;
      reply.redirect(data.images.downsized.url);
    })  
    .catch(function (error) {
      reply.redirect(`https://media.giphy.com/media/M8xmO5ZcLPtAY/giphy.gif`);
    });
})

fastify.get(`/favicon.ico`, (request, reply) => {
  reply.redirect(`https://res.cloudinary.com/jajoosam/image/upload/v1558430146/spongebob-png-44247-Windows_esjjno.ico`);
})

fastify.get(`/:text`, (request, reply) => {
  const text = request.query.text ? request.query.text : request.params.text;
  console.log(request.query);
  const probability = parseFloat(request.query.probability || 0.6);
  var newText = ``;

  for (var i = 0; i < text.length; i++) {
    if (Math.random() >= probability) {
      newText += text.charAt(i).toUpperCase();
    }
    else {
      newText += text.charAt(i);
    }
  }

  reply.send(newText);
})



fastify.listen(2429, `0.0.0.0`)