【问题标题】:How to host Nodejs/Express backend on firebase?如何在 Firebase 上托管 Nodejs/Express 后端?
【发布时间】:2019-01-10 18:51:29
【问题描述】:

我对 NodeJs 和 Firebase 还是很陌生。目前,我有一个带有 Express 和以下文件结构的 NodeJs 项目:

` + /root
    + /db
       - user.js
    + /domain
       - user.js
    + /node_modules
       - ...
    + /routes
       - routes.js
    - sever.js
    - config.json`

我想在 Firebase 上托管它,但它向我添加了文件 index.html 和其他文件。如果我尝试使用 Cloud 函数,它会添加我 /functions 并在其中添加一个“index.js”文件,如下所示:

` + /root
    + /db
       - user.js
    + /domain
       - user.js
    + /functions
       - index.js
    + /node_modules
       - ...
    + /routes
       - routes.js
    - sever.js
    - config.json
    -firebase.json`

我会尝试将我的 server.js 代码粘贴到 index.js 中,但我从引用和缺少的包中得到错误。

这是我的 index.js 代码:

const functions = require('firebase-functions');

var express = require('express');
var bodyparser = require('body-parser');

var app = express();

// Use bodyParser to parse the parameters
app.use(bodyparser.urlencoded({extended: true}));
app.use(bodyparser.json({limit: '10mb'}));

// Call the routes
require('../routes/routes')(app);

exports.api = functions.https.onRequest(app);

还有我的 routes.js 代码:

var userRoute = require('./users');

//Register routes and link to functions
module.exports = function(app){
    app.post('/register', user.userRoute);
}

我的 firebase.json

{
    "hosting" : {
        "public" : "public",
        "rewrites" : [{
            "source" : "/",
            "function" : "api"
        }]
    }
}

当我通过本地运行时

firebase serve --only 函数

工作完美,但问题出在我尝试部署的地方 有什么想法吗?

【问题讨论】:

    标签: javascript node.js firebase express firebase-hosting


    【解决方案1】:

    有一些小众案例可以做到这一点。如果你必须这样做,这是可能的。它记录在Serve Dynamic Content with Cloud Functions

    基本上,您可以通过 firebase.json 在 Firebase 托管中配置重定向。如下所示:

    "rewrites": [ {
      "source": "/myapi", "function": "myapi"
    } ],
    

    然后,您在 Firebase 函数中配置 index.js 文件以侦听该路径上的 HTTP 请求,例如:

    app.get("/myapi", (request, response) => {
      response.set("Cache-Control", "public, max-age=300, s-max-age=2629746");
      var myparam = request.query.item;
        return fetchSomethingFromFirestore(myparam).then((resp) => {
            return response.render("template", {
              myProperty: resp.valueForPugTemplate
            });
        });
    });
    
    app.use(function(err, req, res, next) {
      return errorhandler(err, req, res, next);
    });
    
    const myapi = functions.https.onRequest((request, response) => {
      if (!request.path) {
        request.url = `/${request.url}`;
      }
      return app(request, response);
    });
    module.exports = {
      myapi,
    };
    

    【讨论】:

    • 我想我已经在我的 firebase.json 和 index.js 中做到了。但是当我运行“firebase deploy --only functions”时,我得到了“../routes/routes.js”的缺少包错误
    • 你是从你的函数目录中的终端npm install express -save 吗?你还需要在 index.js 脚本的顶部 require
    猜你喜欢
    • 2022-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-16
    • 2018-01-14
    • 2020-06-17
    • 2020-02-12
    • 2023-03-13
    相关资源
    最近更新 更多