I have a firebase functions folder which I am trying to connect to my frontend app to run calls to my backend server.
The function folder got set up and worked with the emulator, when I tried to use these function in the front end app however they didn't work. The following error showed: Error: Unable to find a valid endpoint for function app
This is what I added to the frontend firebaase.json file:
"hosting": {
"public": "build",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
},
{
"source": "/bigben",
"function": "bigben",
"region": "us-central1"
},
{
"source": "/create-customer-portal-session",
"function": "app"
}
]
}
}
Then this is the index.js file in the functions folder:
const functions = require('firebase-functions');
const express = require('express');
const app = express();
const cors = require('cors')({origin: true});
app.use(cors);
exports.app = functions.https.onRequest(app);
exports.bigben = functions.https.onRequest((req, res) => {
const hours = (new Date().getHours() % 12) + 1 // London is UTC + 1hr;
res.status(200).send(`<!doctype html>
<head>
<title>Time</title>
</head>
<body>
${'BONG '.repeat(hours)}
</body>
</html>`);
});
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY)
const priceId = 'price_1LaJC1CKMdmWgnyspBbFet9v';
const storeItems = new Map([
[
1, { priceInCents: 10000, name: 'Item 1 Name'}
],
[
2, { priceInCents: 20000, name: 'Item 2 Name'}
],
])
app.post("/create-checkout-session", async (req, res) => {
const cust = req.body.customerId
const price = req.body.priceId
const quantity = req.body.students
const quantityA = Number(quantity)
try {
const session = await stripe.checkout.sessions.create({
payment_method_types: ["card"],
mode: 'subscription',
allow_promotion_codes: true,
customer: cust,
line_items: [
{
price: price,
// For metered billing, do not pass quantity
quantity: quantityA,
},
],
success_url: `https://learning-platform.web.app/`,
cancel_url: `https://learning-platform.web.app/`,
})
res.redirect(session.url);
} catch (e) {
res.status(500).json({ error: e.message })
}
})
app.post('/create-customer-portal-session', async (req, res) => {
const cust = req.body.customerId
// Authenticate your user.
const session = await stripe.billingPortal.sessions.create({
customer: cust,
});
console.log(session.url)
res.redirect(session.url);
});
This is what is returned when I run the command: firebase deploy --only functions,hosting --debug

