Cloud Functions for Firebase: 'Error: could not handle the request'

前端 未结 5 1158
慢半拍i
慢半拍i 2020-12-18 18:40

I feel like pulling my hair out; this is either super simple and i\'m having brain freeze or it is not that simple.

What I want

I am trying to unshorten a

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-18 19:30

    First make sure you are receiving the request properly with the shortened url.

    const functions = require('firebase-functions');
    
    const express = require('express');
    var express_app = express();
    express_app.use(body_parser.text({type: ()=>true}));
    express_app.all('*', (req, res) => {
        console.log(req.path);
        res.send(JSON.stringify(req.path));
    });
    exports.url = functions.https.onRequest(express_app);
    

    Now when you visit myapp.firebaseappurl.com/url/SHORTENEDLINK you should see the SHORTENEDLINK in plain text. When that's working, try the redirect.

    const functions = require('firebase-functions');
    const express = require('express');
    const request = require('request');
    var express_app = express();
    express_app.use(body_parser.text({type: ()=>true}));
    express_app.all('*', (req, res) => {
        var url = req.path;
        request({
            uri: uri,
            followRedirect: true
          },
          function(err, httpResponse) {
            if (err) {
              return console.error(err);
            }
            res.send(httpResponse.headers.location || uri);
          }
        );
    });
    exports.url = functions.https.onRequest(express_app);
    

    Also it's good practice to npm install with --save so they end up in the packages.json. While firebase copies your node_modules folder, most other SaaS platforms run npm install.

提交回复
热议问题