LeCoda
LeCoda

Reputation: 1016

Firebase from Centos and express as backend not working

I have decided to use firebase as a backend for authentication and basic form information with firestore.

In the past I've used this express api in cloud functions to do this, and am basing this new setup off of that. But I'm looking to just use it on a Vultr Centos server instead, to put with the rest of my api, and just make everything easier for now as i don't want to overcomplicate things (until later :P).

Now - I've just cloned this repo onto my server, and I want to test it with postman, and i'm having trouble just accessing it.

I'm not sure how to solve the issue and if anyone could point me in the right direction that would make my life so much easier!

here is the index file and the package json file currently. I've created the server.listen to try and make it work at the moment.
const functions = require("firebase-functions");
const app = require("express")();
const FBAuth = require("./util/fbAuth");
const server = require('http').createServer(app);

const cors = require("cors");


//This was recently added to try and make it all work easier!
server.listen(port, ipaddress, () => {
});

app.use(cors());

const { db } = require("./util/admin");

const {
  getAllWorkflows,
...
} = require("./handlers/workflow");

const {
  signup,
  login,
  uploadImage,
  addUserDetails,
  getAuthenticatedUser,
  getUserDetails
} = require("./handlers/users");

// Workflow Routes

app.get("/Workflows", getAllWorkflows);
...
// user route
app.post("/user", FBAuth, addUserDetails);
app.post("/user/image", FBAuth, uploadImage);
...
// cloud functions are better than firebase library because of load time.



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

here is the package.json file.

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "serve": "firebase serve --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "10"
  },
  "dependencies": {
    "busboy": "^0.3.1",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "firebase": "^7.21.1",
    "firebase-admin": "^8.9.0",
    "firebase-functions": "^3.11.0",
    "firebase-tools": "^7.11.0"
  },
  "devDependencies": {
    "firebase-functions-test": "^0.1.6",
    "node-gyp": "^5.0.7"
  },
  "private": true
}

With the backend i am fixing up, I use this sort of workthrough (if it helps!), which I am replacing with firebase stuff above - if that makes sense. It works currently, is accessible for signup and login functionality, the key part for me is just using firebase and firestore with it.

const config = require('../../config');

const express = require('express');
const app = express();
const server = require('http').createServer(app);

.....

server.listen(config.serverParams.port, config.serverParams.address, () => {
    console.log(`Server running at http://${server.address().address}:${server.address().port}`);
});



 ....
    app.use((req,res,next)=>{
        //can reaplce * with website we want to allow access
        res.header('Access-Control-Allow-Origin', 'https://moodmap.app/stats');
      next();
      });
    ....
    io.on('connection', socket => {
    
        socket.use((packet, next) => {
            .....
    
        });
    
    });

Really appreciate any guidance on this matter!

Firebase and firestore seems like a nice way to avoid reinventing the wheel, if only i could simply type npm start, and begin testing with postman the underlying functionality :s

The API is based off another side project i did, which is largely here for those interested in exploring it more.

https://github.com/Hewlbern/LightSpeed/tree/master/sigops

Very happy to just use the easiest way forward - I don't want to have any of the libraries on the client side though, as i want to make my front end super efficient. Thanks!

Upvotes: 0

Views: 157

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317758

Cloud Functions does not allow you to listen on any ports in order to receive requests. It automatically handles incoming connections using the URL automatically provided during deployment. All incoming requests to that URL are routed to your function callback defined by the exported function built by functions.https.onRequest().

If you require the ability to listen to specific ports, Cloud Functions is not the right product for your use case. Cloud Functions can not be deployed on custom servers - it only works on infrastructure provided by Google.

Upvotes: 1

Related Questions