By Gianluca Angioni


2020-05-22 22:46:32 8 Comments

I've wrote an API with Firebase functions.

When I'm testing locally firebase serve --only functions, giving a URL as: http://localhost:5000/my-project/us-central1/app

and firebase serve --only function,hosting giving the URL: http://localhost:5000 I get the responses that I'm expecting, but when I deploy my code (firebase deploy) some routes stop working. After deploying once again without changing my code, the routes that previously worked are now broken and the broken ones are now working.

Using Postman to send the request to my API. testing example:

http://localhost:5000/my-project/us-central1/app/foo/bar

when: https://my-project.web.app/foo/bar The error: Cannot GET /api/articles

index.js:

const functions = require('firebase-functions');

const express = require('express');
const cookieParser = require('cookie-parser');
const cors = require('cors');
const path = require('path');

const api = require('./routes/api');

const app = express();

app.use(express.json());
app.use(express.urlencoded({extended: false}));
app.use(cookieParser());
app.use(cors());

app.use('/api', api.router);

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

api.js

const express = require('express');
const url = require('url');
const articlesRepo = require('../data/articlesRepo');

const router = express.Router();

router.get('/articles', (req, res)=>{
    articlesRepo.getAllArticles((err, articles)=>{
        if (err) res.status(400).json(err.message);
        res.json(articles);
    });
});

router.get('/articles/find', (req, res)=>{
    const queryObject = url.parse(req.url,true).query;

    articlesRepo.findArticlesByName(queryObject, (err, articles)=>{
        if (err) res.status(400).json(err.message);
        res.json(articles);
    });
});

module.exports = {
    router,
};

firebase.json

{
  "hosting": {
    "public": "public",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "**",
        "function": "app"
      }
    ]
  },
  "functions": {
    "predeploy": [
      "npm --prefix \"$RESOURCE_DIR\" run lint"
    ]
  }
}

Edit After checking once again the broken route I got a new anser:

getaddrinfo EAI_AGAIN my-database-host-url:3306

Edit #2

I don't know why the strange behavior by Functions but I've found out that the last error I got was because I'm trying to send traffic to a server that is not google and this is not allowed by google itself unless you don't want to pay for their Spark or other plans.

I suspect that the issues at the beginning where connected to this fact.

0 comments

Related Questions

Sponsored Content

39 Answered Questions

[SOLVED] How do I return the response from an asynchronous call?

3 Answered Questions

[SOLVED] Why this error coming while running Node.js server?

  • 2015-08-11 10:37:30
  • Ramesh T
  • 6015 View
  • 0 Score
  • 3 Answer
  • Tags:   node.js

1 Answered Questions

34 Answered Questions

58 Answered Questions

[SOLVED] How to get the value from the GET parameters?

25 Answered Questions

[SOLVED] In Node.js, how do I "include" functions from my other files?

1 Answered Questions

Cannot have nested routes in firebase cloud function

1 Answered Questions

[SOLVED] Express app using firebase functions

Sponsored Content