最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to host NodejsExpress backend on firebase? - Stack Overflow

programmeradmin3浏览0评论

I'm pretty new with NodeJs and Firebase. Currently, I have a NodeJs project with Express and the following file structure:

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

I want to host this on Firebase but it adds me the files index.html and others. If I try with Cloud functions it adds me /functions and inside this an 'index.js' file, like this:

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

I'd try to paste my server.js code inside the index.js but I get errors from references and missing packages.

This is my index.js code:

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

And my routes.js code:

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

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

My firebase.json

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

When I run local through

firebase serve --only functions

works perfect but the problem es where I try to deploy Any idea?

I'm pretty new with NodeJs and Firebase. Currently, I have a NodeJs project with Express and the following file structure:

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

I want to host this on Firebase but it adds me the files index.html and others. If I try with Cloud functions it adds me /functions and inside this an 'index.js' file, like this:

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

I'd try to paste my server.js code inside the index.js but I get errors from references and missing packages.

This is my index.js code:

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

And my routes.js code:

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

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

My firebase.json

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

When I run local through

firebase serve --only functions

works perfect but the problem es where I try to deploy Any idea?

Share Improve this question edited Aug 3, 2018 at 11:28 ReyAnthonyRenacia 17.7k6 gold badges41 silver badges63 bronze badges asked Aug 2, 2018 at 22:45 DannSaHaDannSaHa 2472 gold badges4 silver badges16 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

There are a few niche cases to do this. Provided you must do this, it is possible. It's documented at Serve Dynamic Content with Cloud Functions.

Basically, you configure a redirect in Firebase Hosting via your firebase.json. Something like below:

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

Then, you configure your index.js file in Firebase Functions to listen for HTTP requests at that path, like:

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,
};
发布评论

评论列表(0)

  1. 暂无评论