简体   繁体   English

React-Firebase托管无法正常工作

[英]React - firebase hosting not working

I`m creating api using react and firebase. 我正在使用React和Firebase创建api。

But there is no hosting. 但是没有托管。 And the Chrome browser requires a cache emptying and a strong refresh to get a 404 page. Chrome浏览器需要清空缓存并进行强大的刷新才能获取404页面。

url is https://jp-db-2018.firebaseapp.com/api/getChannels 网址是https://jp-db-2018.firebaseapp.com/api/getChannels

in conclusion I want to use react and firebase for hosting. 总之,我想使用react和firebase进行托管。

Here are the project settings. 这是项目设置。

Thank you for your advice. 感谢您的意见。

firebase.json firebase.json

{
  "firestore": {
    "rules": "firestore.rules",
    "indexes": "firestore.indexes.json"
  },
  "functions": {
    "predeploy": [
         "npm --prefix \"$RESOURCE_DIR\" run lint"
    ],
    "source": "functions"
  },
  "hosting": {
    "public": "build",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "/api/**",
        "function": "api"
      }
    ],
    "headers": [ {
      "source" : "**/*.@(eot|otf|ttf|ttc|woff|font.css)",
      "headers" : [ {
        "key" : "Access-Control-Allow-Origin",
        "value" : "*"
      } ]
      }, {
     "source" : "**/*.@(jpg|jpeg|gif|png)",
      "headers" : [ {
        "key" : "Cache-Control",
        "value" : "max-age=7200"
      } ]
      }, {
      "source" : "404.html",
      "headers" : [ {
        "key" : "Cache-Control",
        "value" : "max-age=300"
      } ]
    } ],
    "cleanUrls": true,
    "trailingSlash": false
  }
}

/src/index.js /src/index.js

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';
import * as firebase from 'firebase';

const config = {
  apiKey: "xxxxxxxx",
  authDomain: "xxxxxxxx",
  databaseURL: "xxxxxxxx",
  projectId: "xxxxxxxx",
  storageBucket: "xxxxxxxx",
  messagingSenderId: "xxxxxxxx"
};
firebase.initializeApp(config);

ReactDOM.render(<App />, document.getElementById('root'));
registerServiceWorker();

/functions/index.js /functions/index.js

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const serviceAccount = require('./serviceAccountKey.json');
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://jp-db-2018.firebaseio.com"
});
const express = require('express');
const app = express();
const db = admin.firestore();

app.get('/getChannels', (req, res) => {
  res.json({code: 200});
});
exports.api = functions.https.onRequest(app);

tree view 树视图

在此处输入图片说明

在此处输入图片说明

Seems to be working fine. 似乎工作正常。 You specified that you want it to say code:200 in a json response in your firebase function. 您在Firebase函数的json响应中指定了要让它说出code:200 Were you expecting something else? 您还期待其他吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM