Express CANNOT POST after firebase function deployment

Viewed 117

I can make this method to work in local but i get CANNOT POST / and CANNOT GET / after deploying to firebase. Body of the POST and GET method is the ipify api to get information about the requesting IP.

  • My local node.js version is v16.13.1
  • Firebase node.js version is v14
  • My Firebase is on Blaze (Pay as you go) plan

Running and working fine in Local and tested with POSTMAN tool:

const request = require('request');
const express = require('express');
const bodyParser = require('body-parser');

const app = express();
app.use(bodyParser.urlencoded({ extended: false })); //application/x-www-form-urlencoded
app.use(bodyParser.json()); //application/json
app.route('/post').post((requests, responses) => {
  request('https://api64.ipify.org/?format=json', { json: true }, (err, ipfyResponse, body) => {
    responses.send(`
    ipfyResponse.statusCode: ${ipfyResponse.statusCode}\n
    ipfyResponse.statusMessage: ${ipfyResponse.statusMessage}\n
    `);
    console.log(ipfyResponse.body);
    console.log(ipfyResponse.headers);
    console.log(requests.body);
    console.log(requests.headers);
  });
});
app.listen(8080, () => console.log(`Server at localhost:8080`));

The function that i am deploying and gives me CANNOT POST and CANNOT GET response:

const functions = require("firebase-functions");
const httprequest = require('request');
const express = require('express');
const bodyParser = require('body-parser');

const app = express();
app.use(bodyParser.urlencoded({ extended: false })); //application/x-www-form-urlencoded
app.use(bodyParser.json()); //application/json
app.post('/post', (requests, responses) => {
  httprequest('https://api64.ipify.org/?format=json', { json: true }, (err, ipfyResponse, body) => {
    if (err) { return console.log(err); }
    responses.send(`
    ipfyResponse.statusCode: ${ipfyResponse.statusCode}\n
    ipfyResponse.statusMessage: ${ipfyResponse.statusMessage}\n
    `);
  });
  console.log(ipfyResponse.body);
  console.log(ipfyResponse.headers);
  console.log(requests.body);
  console.log(requests.headers);
});
exports.post = functions.https.onRequest(app);

app.get('/get', (requests, responses) => {
  httprequest('https://api64.ipify.org/?format=json', { json: true }, (err, ipfyResponse, body) => {
    if (err) { return console.log(err); }
    responses.send(`
    ipfyResponse.statusCode: ${ipfyResponse.statusCode}\n
    ipfyResponse.statusMessage: ${ipfyResponse.statusMessage}\n
    `);
  });
  console.log(ipfyResponse.body);
  console.log(ipfyResponse.headers);
  console.log(requests.body);
  console.log(requests.headers);
});
exports.get = functions.https.onRequest(app);

Im testing my POST firebase function with:

curl -X POST -d key=value --ssl https://us-central1-somethinsomethin.cloudfunctions.net/post

And testing the GET firebase function with my web browser (Edge)

0 Answers
Related