Abdulhakeem Imran
Abdulhakeem Imran

Reputation: 138

How to fix “Cannot GET” Error on Firebase Cloud Functions

I have successfully configured my firebase. Inside my firebase functions folder I have the index.js which I have edited. Below is the code:

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

const {
    getAllTodos
} = require('./APIs/todos')

app.get('/todos', getAllTodos);
exports.api = functions.https.onRequest(app);

I have also created a todos.js file under APIs directory in the functions folder. I have written the getAllTodos function.

exports.getAllTodos = (request, response) => {
    todos = [
        {
            'id': '1',
            'title': 'greeting',
            'body': 'Hello world from sharvin shah' 
        },
        {
            'id': '2',
            'title': 'greeting2',
            'body': 'Hello2 world2 from sharvin shah' 
        }
    ]
    return response.json(todos);
}

I have successfully deployed it on my firebase. Using the ULR generated to view

https://us-central1-todoapp-f665a.cloudfunctions.net/api

I keep getting a

Cannot GET /

What could be wrong with my code? I have checked other questions that have to do with Cannot GET / error to no avail.

Thanks in advance for the help.

Upvotes: 0

Views: 430

Answers (1)

michael
michael

Reputation: 4173

/api is the basic route of your express app. You should add /todos to get the expected response.

https://us-central1-todoapp-f665a.cloudfunctions.net/api/todos

Upvotes: 1

Related Questions