We will now deploy our weather app to the cloud. However there are 2 parts to the app. The react client and your API server.
The react client will be deployed to Firebase Hosting as a static site. A static site is simply a location to store the html, javascript, and css that make up our react application.
The weather API that we created will be deployed to a Serverless Function. We will only deploy the route that fetches the weather for the city and returns daily weather data as JSON.
Give your project a unique name and select the university parent organization:

Disable google analytics:

Start in the root of your weather-app project (the same directory as your package.json file).
npm run build
Note: you now have a build directory containing the files required to deploy your application.
npm install --save-dev firebase-tools
Add a npm run-script to make it easier to run.
package.json:
1
2
3
4
5
6
7
8
// ...
"scripts": {
  // ...
  "firebase": "firebase"
},
// ...
npm run firebase -- --help
npm run firebase -- login
npm run firebase -- init
Hosting with the space bar and then press enter.don't setup a default project and press enterdist, the directory containing your built react application.y because our react application is a single page application.n.n since we want to use the index.html generated by the react build process.💡 Note that Firebase added 2 new files, .firebaserc that remembers which firebase project to use by default and firebase.json that contains some firebase settings, including those that you configured above.
npm run firebase -- use --add
default and press enternpm run firebase -- deploy
Navigate to the URL provided by firebase in console. It should resemble: https://{your project name}.web.app
Try accessing your site with smartphone. You just deployed your first site, with SSL, to the cloud… for free!
npm run firebase -- init
nnModify your functions to use the node.js version 18 runtime.
In functions/package.json modify the engines entry:
1
2
3
"engines": {
  "node": "18"
},
cd functions
npm install
In vscode, open functions/index.js and un-comment all of the lines of code. Then rename the function to weather:
1
2
3
4
exports.weather = functions.https.onRequest((request, response) => {
functions.logger.info("Hello logs!", {structuredData: true});
response.send("Hello from Firebase!");
});
/firebase.json file. Add a rewrite rule so that you function is accessible in the same domain as your hosting. Your rewrite rules should resemble the following:
    1
2
3
4
5
6
7
8
9
10
"rewrites": [
  {
    "source": "/api/weather",
    "function": "weather"
  },
  {
    "source": "**",
    "destination": "/index.html"
  }
]
cd ..
npm run firebase -- deploy
⚠️ You will get an error message the first time you deploy your function:
Error: Your project weather-cpinfo20 must be on the Blaze (pay-as-you-go) plan to complete this command.
Follow the link and add purchase the firebase “Blaze” plan using the billing account created with the free credits that you received from Google Cloud.

After upgrading the project, re-execute npm run firebase -- deploy.
You just deployed your first serverless cloud function! You can access either directly in the URL listed in the console:
https://us-central1-{your project name}.cloudfunctions.net/weather
Or at the path that we configured in the hosting (notice the path is api):
https://{your project name}.web.app/api/weather
☝️ Take a moment to appreciate what we now have. A website deployed, with javascript backend configured to exact routes. Plus, we never configured a server, virtual machine, docker, or paid for hosting!
💡 The function onRequest() works the same way as a route in express with the request and response parameters.
getWeather() function from your express app into functions/index.js and call it from the weather function:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const functions = require('firebase-functions');
exports.weather = functions.https.onRequest(async (request, response) => {
  const city = request.query.city;
  functions.logger.info("Getting weather for city", {city});
  const weather = await getWeather(city);
  functions.logger.info("Fetch weather data", {weather});
  response.send(weather);
});
async function getWeather(city) {
  // Your getWeather() function...
  
  // 1. get long/lat for city
  // 2. get weather for long/lat
  // ...
}
async keyword was added to the function!/api/weather?city=annecy. It can be accessed in the function in request.query.city. Firebase docs npm run firebase -- emulators:start
 npm run firebase -- deploy
