Covert firebase to Node Express Rest API
Firbase hosting and functions provide nice solution to develop a REST api .The following video from firebase provide more infromation than our simple REST API.
https://www.youtube.com/watch?v=LOeioOKUKI8
I thought of detail the process in to more depth.
First , you need to go to desired folder and install firebase tools. You should have npm for do that.
Then create a firebase account and project with desired name.
After that, you have to use the command firebase init.
The online authentication will be proceed there after .You have to copy and paste the URL displayed in the console to the web browser.
Then there we have to create the local project with firebase hosting and firebase functions.
In the project you can see the functions folder ,which used to create the functions.
In order to use the express, you need to use express npm packge.
Move to functions folder and then run npm install express, then firebase-functions. If you are using firebase database , you need to add "firebase" packge too.
Within the index file, you can proceed with following.
// intiatiate the express
const app = express();
// intiatiate the firebase with the current cardinals of the functions project
firebaseAdmin.initializeApp(functions.config().firebase);
You can create a sample request
app.get('/timestamp',(request,res)=>{
});
Then finally add the following
exports.app = functions.https.onRequest(app);
In the firebase.json file , which is the configuration file, you have to add the following code.
{
"hosting": {
"public": "public",
"rewrites":[{
"source":"**",
"function":"app"
}],
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
]
}
}
"firebase serve" is the command for locally run the code .
After testing locally, you can run "firebase deploy" to deploy the code.Sucessful deployment will indicate the "hosting" section in firebase Console.
You can see the logs on deployed app in the Log Tab of the "Functions" section.
https://www.youtube.com/watch?v=LOeioOKUKI8
I thought of detail the process in to more depth.
First , you need to go to desired folder and install firebase tools. You should have npm for do that.
Then create a firebase account and project with desired name.
After that, you have to use the command firebase init.
The online authentication will be proceed there after .You have to copy and paste the URL displayed in the console to the web browser.
Then there we have to create the local project with firebase hosting and firebase functions.
In the project you can see the functions folder ,which used to create the functions.
In order to use the express, you need to use express npm packge.
Move to functions folder and then run npm install express, then firebase-functions. If you are using firebase database , you need to add "firebase" packge too.
Within the index file, you can proceed with following.
// intiatiate the express
const app = express();
// intiatiate the firebase with the current cardinals of the functions project
firebaseAdmin.initializeApp(functions.config().firebase);
You can create a sample request
app.get('/timestamp',(request,res)=>{
});
Then finally add the following
exports.app = functions.https.onRequest(app);
In the firebase.json file , which is the configuration file, you have to add the following code.
{
"hosting": {
"public": "public",
"rewrites":[{
"source":"**",
"function":"app"
}],
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
]
}
}
"firebase serve" is the command for locally run the code .
After testing locally, you can run "firebase deploy" to deploy the code.Sucessful deployment will indicate the "hosting" section in firebase Console.
You can see the logs on deployed app in the Log Tab of the "Functions" section.
Comments
Post a Comment