React - firebase托管不起作用

时间:2018-03-20 09:45:47

标签: javascript reactjs firebase google-cloud-functions firebase-hosting

我正在使用react和firebase创建api。

但没有托管。 Chrome浏览器需要缓存清空和强大的刷新才能获得404页面。

网址为https://jp-db-2018.firebaseapp.com/api/getChannels

总之,我想使用react和firebase进行托管。

以下是项目设置。

感谢您的建议。

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

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

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);

树视图

enter image description here

1 个答案:

答案 0 :(得分:-1)

enter image description here

似乎工作正常。您指定要在firebase函数的json响应中说出code:200。你期待别的东西吗?