2
votes

I am building a simple web app using node js express and cloud firebase functions

i have 2 endpoints, 1. first point renders the form(GET request) and 2. POST endpoint that takes the form data on submit

for some reason firebase skipping the function name in the post url(2nd end point) on form submission but it works fine on local express server

example: if form acion attribute value is "check" and firebase function name is "helloWorld"

the form submit url should be "<default firebase pefix>/helloWorld/check" but instead the url it posting to is "<default firebase pefix>/check". firebase function name in the url is getting skipped. which is giving me function not found in location us-central etc

another thing i observed is if i give slash as prefix to action attribute value like "action = "\check". firebase skipping the whole base url and appending from attribute value the port value

i tried a work around by setting the static absolute path (path after production) to the form action attribute.

But i want to if its a bug or am i missing something

<form action="check" method="POST"   

    <label for="uname"><b>Username</b></label>
    <input type="text" placeholder="Enter Username" name="uname" required>     
    <button type="submit">Login</button> 

</form>

// action = "/check" this is skipping total base url all together

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

const express = require('express')
const bodyparser = require('body-parser')
const app = express()
app.set('port',6000)
app.use(bodyparser.json())
app.use(bodyparser.urlencoded({extended:true}))
app.set('view engine', 'ejs');

app.get('/',(req,res)=>{
  res.render('formfill')
})

 // this below end point is supposed to get triggered on form submission.
// and it is working fine on local express server, but not on firebase functions

app.post('/check',(req,res)=>{

res.send(`you said ${req.body.uname}`)
})

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

2 Answers

1
votes

You can't use Cloud Functions to run a web server or listen on a port. All your code that's trying to run express is not going to work. When you deploy an HTTP function, it's assigned a URL, and you use that URL as the endpoint for requests.

You should review the documentation for HTTP triggers to better understand how this works.

1
votes

I solved it. i don't know if it is my mistake or lack of knowledge.

so the firebase functions url is something like this

https://us-central1-<project-id>.cloudfunctions.net/<functionname>

in the local firebase server if i go to the functions url(localhost prefix) with or without a slash at the end of the url. my root endpoint is getting consumed. which is fine.

but it is not the case in production url, a slash at end of url(after my function name) is required to load the endpoint. and any anchor tag href in the webpage should omit the prefix slash

example: action = "/check" this is not working but action ="check/" this is working

so i just removed prefix slash in my action attribute and re deployed, now it is working.