javascript - How to use a custom domain for a Cloud Function as a POST request - Stack Overflow

I'm not very experienced with Node.js but learning quick all do quite good with javaScript. I'

I'm not very experienced with Node.js but learning quick all do quite good with javaScript. I'm using Cloud Functions to create an API for a project and trying to use a custom domain to reach this API. On my Firebase Hosting, I have connected a subdomain "api.mydomain".

I have a function called "api" on my functions index.js using express:

let express = require('express');
let app = express();

app.post('/endpoint/:userId', (req, res) => {

  ... EXECUTE CODE 

  res.json(json);  

});

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

In my firebase.json I have a rewrite as so:

    "rewrites": [
  {
    "source": "/api/**",
    "function": "api"
  }

So in theory if I make a POST request to /api/endpoint/userID should execute the function but instead I get:

Cannot POST /api/endpoint/userID/

If I use the default firebase URL to access the function like it works fine.

Do you have any Idea how to properly configure the custom domain to work with my function?

Thanks a lot for any help!

I'm not very experienced with Node.js but learning quick all do quite good with javaScript. I'm using Cloud Functions to create an API for a project and trying to use a custom domain to reach this API. On my Firebase Hosting, I have connected a subdomain "api.mydomain.".

I have a function called "api" on my functions index.js using express:

let express = require('express');
let app = express();

app.post('/endpoint/:userId', (req, res) => {

  ... EXECUTE CODE 

  res.json(json);  

});

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

In my firebase.json I have a rewrite as so:

    "rewrites": [
  {
    "source": "/api/**",
    "function": "api"
  }

So in theory if I make a POST request to https://api.mydomain/api/endpoint/userID should execute the function but instead I get:

Cannot POST /api/endpoint/userID/

If I use the default firebase URL to access the function like https://us-central1-my-proyect.cloudfunctions/api it works fine.

Do you have any Idea how to properly configure the custom domain to work with my function?

Thanks a lot for any help!

Share Improve this question edited Dec 26, 2017 at 16:34 Doug Stevenson 319k36 gold badges456 silver badges473 bronze badges asked Dec 26, 2017 at 15:58 XazzoXazzo 851 gold badge1 silver badge6 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

When you use an Express app as the target for an HTTPS function, the name of the function gets prepended to the path of the hosting URL, just like it does when you call the function direction. There are two ways to pensate for this:

  1. Put the prefix path in your route paths:

    app.post('/api/endpoint/:userId', (req, res) => { ... })
    
  2. Create a second Express app that routes everything under /api, and send that to Cloud Functions:

    app.post('/endpoint/:userId', (req, res) => { ... })
    const app2 = express()
    app2.use('/api', app)
    exports.api = functions.https.onRequest(app2)
    

Either way, when you rewrite path /api/** to function api, your function will get invoked.

My preferred method is to remove the prepend and continue with the normal flow.

If I want to call mysite./api/process

const PREFIX = "api"
app.use((req, res, next) => {
  if (req.url.indexOf(`/${PREFIX}/`) === 0) {
    req.url = req.url.substring(PREFIX.length + 1);
  }
  next();
});

app.post("/process", (req, res) => {...}

exports[PREFIX] = functions.https.onRequest(app);

and in firebase.json

"rewrites": [
      {
        "source": "/api/**",
        "function": "api"
      },
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745370206a4624763.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信