0

I want to separate/create routes in firebase hosting using Node js. I have created two JS files: apiservice.js and service.js which contain firebase cloud functions.

apiservice.js file contains routes related to API requests. service.js file contains routes related to normal use requests. When I run this code, the below error is coming:

Cannot GET /firebaseproject-id/us-central1/app/api/

Code is given below:

apiservice.js:

const functions = require('firebase-functions');
const express = require("express");
const app = express();
app.get("/api/login", (req, res) => {
    //code
});
app.get("/api/signup", (req, res) => {
    //code
});
exports.apiservice = functions.https.onRequest(app);

service.js:

const functions = require('firebase-functions');
const express = require("express");
const app = express();
app.get("/signup", (req, res) => {
    //code
});
app.get("/login", (req, res) => {
    //code
});
exports.service = functions.https.onRequest(app);

firebase.json:

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

Any help will be appreciated. Thank you.

Bhuvan Gandhi
  • 167
  • 4
  • 10
  • How did you deploy that code? Normally, there is a single index.js that exports all the functions for the project. These exports are the only things that can be destinations for rewrites. – Doug Stevenson Apr 07 '20 at 16:00
  • Cool. How should I write code that these both files contain separate code for api and normal use and I just need to add them in index.js file? – Bhuvan Gandhi Apr 07 '20 at 16:58
  • It sounds like you need to formulate a whole new question to explain what you're trying to do now. – Doug Stevenson Apr 07 '20 at 17:15
  • Yes. You are correct. Actually I was asking a question in the wrong way. I was looking for separating routes in different JS files and combine them in index.js file. Fortunately, I got the solution. Thank you for your assistance. Link of solution: https://stackoverflow.com/questions/6059246/how-to-include-route-handlers-in-multiple-files-in-express/37309212#37309212 – Bhuvan Gandhi Apr 08 '20 at 05:25
  • Glad you found an answer. Could you now delete this question, or answer it with your own solution? – Doug Stevenson Apr 08 '20 at 05:52
  • Sure. I will answer this question. – Bhuvan Gandhi Apr 08 '20 at 05:59

1 Answers1

1

I have found a solution. I was not importing apiservice and service in the index.js file. Because of that, the routing was not working. So, I changed my code a little bit and added a few lines in the index.js file. I got the reference from here: How to include route handlers in multiple files in Express?

So, the updated working code is:

apiservice.js:

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

let apirouter = express.Router();
apirouter.get("/api/signup", (req, res) => {
    //Code
});
module.exports = apirouter;

service.js:

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

let router = express.Router();
apirouter.get("/signup", (req, res) => {
    //Code
});
module.exports = router;

index.js:

const functions = require('firebase-functions');
const express = require("express");
const apisrcrouter = require("./apiservice");
const srcrouter = require("./service");

let app = express();
app.get("/api/**", apisrcrouter);
app.get("/**", srcrouter);

module.exports = {
    'apisrcrouter': functions.https.onRequest(apisrcrouter),
    'srcrouter': functions.https.onRequest(srcrouter)
};

firebase.json:

{
    "hosting": {
        ...
        "rewrites": [
        {
            "source": "/api/**",
            "function": "apisrcrouter"
        },
        {
            "source": "/**",
            "function": "srcrouter"
        }]
    ...
}
Bhuvan Gandhi
  • 167
  • 4
  • 10