美文网首页js css html
firebase deploy functions & host

firebase deploy functions & host

作者: Jason_风筝 | 来源:发表于2020-01-03 17:49 被阅读0次
  • 用node , deploy functions 到firebase, 废话不多说, 开始吧
1. firebase init functions 
2. firebase init hosting
3. cd functions -> npm i firebase-functions express --save

如果还没安装tools, 请:  npm install -g firebase-tools
它会创建functions 文件夹, code 写在fucntions 中, 结构如下图


  • firebase.json 如下
{
    "hosting": {
        "public": "public",
        "rewrites": [
            {
                "source": "**",
                "function": "app"
            }
        ]
    }
}

其中,  "function": "app" , 为提供出来的function, ** 表示, 任何请求都会走它
  • index.js
const functions = require('firebase-functions');
const express = require('express');
const app = express()

app.get('/timestamp', function (req, res) {
    res.send(`${Date.now()}`);
})

app.get('/timestamp-cached', function (req, res) {
    /// 让响应缓存 
    res.set('Cache-Control','public, max-age=300, s-maxage=600');
    res.send(`${Date.now()}`);
})
exports.app = functions.https.onRequest(app);
  • 以上的话, 就定义了两个简单的方法: timestamp / timestamp-cached

  • 本地run

firebase serve --only functions,hosting
  • deploy 的话
firebase use --clear
firebase use xxx(你的firebase project id)
firebase deploy --only functions,hosting
  • 到此你的functions depoly 完成:
    可以用以下访问到, 如果想return 回json之类的, 肯定也可以: res.json({"key": "value"})

    • https://..../app/timestamp
    • https://..../app/timestamp-cached
  • 注意: 静态文件夹(public)中不能有index... , 默认静态文件夹是Public

Code

相关文章

网友评论

    本文标题:firebase deploy functions & host

    本文链接:https://www.haomeiwen.com/subject/saxtactx.html