0
votes

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.

1
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
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
It sounds like you need to formulate a whole new question to explain what you're trying to do now.Doug Stevenson
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: stackoverflow.com/questions/6059246/…Bhuvan Gandhi
Glad you found an answer. Could you now delete this question, or answer it with your own solution?Doug Stevenson

1 Answers

1
votes

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"
        }]
    ...
}