0

I have developed a nodejs app that can convert HTTP/HTTPS links to torrents. It is working fine on localhost. When I upload it to firebase hosting. it's not working. It's even working on firebase localhost which I created using. How can i fix this.

firebase serve command

but when I deployed it to the firebase server it is showing the index page. but it is not working.

below is my sourcecode.

firebase.json file inside main folder

{
  "hosting": {
    "public": "./public/html",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      
      {
        "source": "/?thelink=",
        "destination": "app"
      },
      {
        "source": "/l?thelink=",
        "destination": "app"
      },
      {
        "source": "/",
        "destination": "app"
      }
    ]
  }
}

index.js on function folder

const functions   = require('firebase-functions');
var http          = require('http');
var webtorrentify = require('webtorrentify-link');
var fs            = require('fs');
var url           = require("url");
var path          = require("path");
var validUrl      = require('valid-url');
var express       = require('express');
var getUrls       = require('get-urls');
var remote        = require('remote-file-size');
var app           = express();

var downloadLink;
var fileName;
var fileSize;
var server;
var parsed;
var param;
var link;
var port;
port = process.env.PORT || 5000;

app.get('*', function(req, res){
    if(req.url==='/'){
      //app.use('/public/html', express.static(path.join(__dirname)));
    fs.readFile('public/html/index.html', function (err, data) {
    res.write(data);  
  });
  }else if (req.url === '/l?thelink='){
    fs.readFile('public/html/emptyRequest.html', function (err, data) {
      res.write(data);
      res.end();
  });
}else{
//---------Reciving Url--------------------
  console.log(req.query.thelink);
  downloadLink=req.query.thelink;
  //-----------------------------------------

  //------------checking for valid url-------
  if (validUrl.isUri(downloadLink)) {
    console.log('Looks like an URI');
    //-----------------------------------------
    
    //----------Extracting filename-------------
    parsed = url.parse(downloadLink);
    fileName = path.basename(parsed.pathname);
    console.log(path.basename(parsed.pathname));
    //-------------------------------------------
    
    //----------Finding File size----------------
    remote(downloadLink, function(err, o) {
      fileSize = (o/1024)/1024;
      console.log('size of ' + fileName + ' = ' + fileSize+" MB");  
    //-------------------------------------------
    if (fileSize < 501)
    {
    ///////////////Creating Torrent////////////////////
    webtorrentify(downloadLink)
      .then(function (buffer) {
         console.log('creating the torrent');
         //res.send('what is');
         //-------------------------------------------
         res.setHeader('Content-Type', 'application/x-bittorrent');
         res.setHeader('Content-Disposition', `inline; filename="${fileName}.torrent"`);
         res.setHeader('Cache-Control', 'public, max-age=2592000'); // 30 days
         res.send(buffer);
         console.log(fileName+'.torrent created');
         res.end();
         //-------------------------------------------
      });
    ////////////////////////////////////////////////
    }
    else{
      console.log('More than 500 MB');
      res.send("<h4> More than 500 MB or invalid URL </h4>");
    }
  });
  }
  else {
    console.log('not url');
    fs.readFile('404.html', function (err, data) {
      res.write(data);
      res.end();
    });
    
  }
}
});

app.listen(port);

console.log('server up and running', port);

exports.app = functions.https.onRequest(app)

index.html on folder public/html

<html>
<title> FireBit </title>

            <script type="text/javascript">
                function operacion(){
                        var link = document.getElementById("link").value;
                        location.href = "https://firebit-0.web.app:5000/firebit-0/us-central1/app"+"/?thelink="+ link;
                }
            </script>       
    </head>

    <body>
            <h1>input url </h1>
            <form id="forma1"> 
                <input type="string" id="link"></imput>
                <input type="button" onClick="operacion()" id="enviar" value="burn"></input>
            </form>
    </body>

</html>
Savad
  • 1,240
  • 3
  • 21
  • 50
  • 1
    Have you checked the versions of Node? What version are you running and which one is the server running? Maybe there is an issue there? – covo Jul 22 '20 at 14:29
  • yes... some other projects are working fine with the same version of node – Savad Jul 24 '20 at 05:12

0 Answers0