I have a standard REST API using Express, exposed via a Firebase Cloud Function.
const api = express()
api.get('/test', (req, res) => res.status(403).json({ "REASON": "UNAUTHORIZED" }))
exports.api = functions.https.onRequest(api)
Remote
When I deploy it and send GET https://<remote>/api/test via Postman I get 403 { "REASON": "UNAUTHORIZED" } as expected.
Local
When I run firebase emulators:start --only functions to serve these functions locally and test them, I do see functions: HTTP trigger initialized at http://localhost:5001/.../api in my terminal, but when I send GET http://localhost:5001/.../api/test via Postman I get 200 Not Found.
Am I missing something?
It was indeed a defect in the Firebase CLI, it seems to be fixed in the latest version (7.13.1)
Installed the latest version using: npm install -g firebase-tools#latest
Note: it did require me to update to compatible version of firebase-functions and firebase-admin as well.
Related
My app on SvelteKit + ts. I build npm run build (node adapter), with npm run preview (and with npm run dev) everything works. With node build
export async function post ({body}) {
try {
const login = body.get ('login');
error TypeError: body.get is not a function.
Content-Type request: application / x-www-form-urlencoded. Body is Uint8Array(34) in building app.
As I understand it, you need to screw the bode-parser somewhere. Or how to solve this?
Thanks!
Update #sveltejs/kit to 1.0.0-next.165 fix it =)
I am trying to develop API for my apps using Firebase cloud functions.
Following this site to use the firebase emulator suite for development and testing locally.
Issue: The changes are not reflected in the locally emulated functions.
Steps:
index.js:
exports.test = functions.https.onRequest(async (request, response) => {
response.status(200).send("First");
});
Successfully deployed the test method.
firebase deploy --only functions:test
In Postman made the following GET request.
https://us-central1-<project-name>.cloudfunctions.net/test
Result: First
Status: 200 OK
Started the emulators:
firebase emulators:start --only functions
In Postman made the following GET request.
http://localhost:5001/<project-name>/us-central1/indexTest
Result: First
Status: 200 OK
Same as the actual deployed function.
Changed the function code to:
exports.test = functions.https.onRequest(async (request, response) => {
response.status(200).send("Second");
});
Getting the same result as before when hitting the emulated function in localhost. The changes are not reflected.
Also, tried stopping the emulator and starting it again. No luck.
I had raised an issue in the firebase-tools repo as suggested by DougStevenson.
Got the issue resolved with Sam Stern's support.
Posting the solution here for anyone else who gets stuck in the same issue.
Solution:
After every change to the ts files, run "npm run build" to compile the code again.
Change "build": "tsc" to "build": "tsc -w" in package.json if you want to auto-compile after every change.
I have already created backend using node js for sigin & signup page. Now I want to connect to node js . But i have no idea how to do that. I want to connect both react native with my node js. Can you help me ?
simply as how we do for web apps.
here is an example of error reporting
export default async function (body) {
console.log(JSON.stringify(body))
const res = await fetch(`${host}/api/report`, {
method: 'POST',
body: JSON.stringify(body),
headers: {
'Content-Type': 'application/json',
},
})
const { message } = await res.json()
if (message) return Toast({ message: message });
else return Toast({ message: 'network error' });
}
I have used fetch to send a POST request to my nodejs server
use API tool like postman or other and make your your nodejs APIs works fine and then connect to your React Native app as above.
You can use ngrok to connect Node with react-native. Run this command:
npm i ngrok -g # installing it globally
Then open another terminal. Run:
ngrok http 3000 # the port you are running on node
Then it will show an alternative link that you can use to test with your Node.
Note: if ngrok http 3000 doesn't work, try ngrok http -region us 3000.
The available ones are us, eu, ap, and au. In my case eu worked for me.
Then copy the link generated e.g. http://8074-129-205-124-100.eu.ngrok.io and test your backend if it displays APIs.
If the link works then you can use it with fetch. Uploading json data to send to MongoDB as the case maybe.
I'm trying to see if it's possible to integrate Cloud Functions for Firebase with Braintree. I created a project for Cloud Functions according to the docs.
In the project directory I ran: npm install braintree.
I modified index.js for testing purposes to be the following:
const functions = require('firebase-functions');
var braintree = require("braintree");
var gateway = braintree.connect({
environment:
braintree.Environment.Sandbox,
merchantId: "useYourMerchantId",
publicKey: "useYourPublicKey",
privateKey: "useYourPrivateKey"
});
// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-
functions
//
exports.helloWorld = functions.https.onRequest((request, response) => {
response.send("Hello from Firebase!");
//gateway.clientToken.generate({}, function (err, response) {
//response.send(response.clientToken);
//});
});
When I tried to deploy this test function I got the error
Error parsing triggers: Cannot find module 'braintree'
I'm new to Firebase, Cloud Functions, and node.js and would appreciate any input on how to import Braintree to Firebase Functions project.
It looks like Cloud Functions for Firebase is not picking up the braintree module. Like most Node.js environments, Cloud Functions reads the dependencies from package.json. When you install a module with npm you can tell it to also write it to package.json by adding --save to the command line. So:
npm install braintree --save
You have the Node.js package braintree missing.
Your Firebase project has a directory called functions.
In the terminal, go to the functions directory by $ cd {your project dir}/functions
And then npm i braintree --save.
I hope that it helps you.
change current directory to functions:- cd functions
then install braintree in that folder using npm: - npm i braintree
then import braintree : - var braintree = require('braintree');
now everything should work fine.
Note:- if you have not enabled payment in firebase, it will though 'unexpectedError' in the http response.
I am trying to access Realtime Database using the new feature introduced with 3.1.0 release: "The Node.js SDK now supports unauthenticated access. If no service account is provided, Realtime Database access will be restricted just as any unauthenticated client would be."
The SDK is updated to 3.1.0:
user#ha:~/dev/project/auth/firebase$ sudo npm install -g firebase
[sudo] password for user:
/usr/local/lib
└─┬ firebase#3.1.0
Tried with no service account:
var firebase = require('firebase');
console.log('Initialise Firebasse app');
firebase.initializeApp({
// serviceAccount: "",
databaseURL: "https://some-valid-firebase.firebaseio.com"
});
The result is:
user#ha:~/dev/project/auth/firebase$ nodejs fb_anon.js
Initialise Firebasse app
/home/user/dev/project/node_modules/firebase/auth-node/auth.js:61
throw new Error('Invalid service account provided');
^
Please help, probably I am missing something obvious here. :-(
What is happening is that your application is referring to an old sdk version that is inside your node_modules. And thats because you updated the firebase sdk with the globally flag and you are referring to a local sdk.
You should be installing it locally to your application.
First make sure you added "firebase":"3.1.0" into your package.json file and then call npm install inside the same dir.
I've just tested it with the same piece of code you added and everything went as expected.
In this question you can find some additional information and tips when using npm.