0
votes

I'm using firebase Functions to display a NodeJS + Express and to separate requests from the API-Rest I am separating several functions as follows:

const auth     = require('./functions/auth');
const user     = require('./functions/user');
const msgtypes = require('./functions/msgtypes');
const msg      = require('./functions/msg');
const comment  = require('./functions/comment');
//const test = require('./functions/test');

exports.auth      = auth.app
exports.user      = user.app
exports.msg       = msg.app
exports.msgtypes  = msgtypes.app  
exports.comment = comment.app 

Each Function it´s something like this: (User Functions) const app = require('./app');

app.get('/me',      general.logUrl,                                ctrl.me)
app.put('/images',  general.logUrl,general.isLoged, upload.any() , ctrl.removeImage)
app.post('/images', general.logUrl,general.isLoged, upload.any() , ctrl.uploadImage)
app.get('/images',  general.logUrl,general.isLoged,                ctrl.getImages)
//app.get('/',                         ctrl.get)
app.get('/notifs',  general.logUrl,                 ctrl.getMyNotifs)
app.post('/',       general.logUrl,general.isAdmin, ctrl.createUser)
app.post('/profile',general.logUrl,                 ctrl.publicProfile)
app.post('/contact',general.logUrl,                 ctrl.contactUser)
app.get('/:id',     general.logUrl,general.isAdmin, ctrl.getData)


app.post('/status/:id', general.logUrl,general.isAdmin, ctrl.status)  
app.put('/',            general.logUrl,general.isLoged, ctrl.update)
app.post('/edit',       general.logUrl,general.isLoged, ctrl.update)
app.post('/change/:id', general.logUrl,general.isLoged, ctrl.change)
app.post('/changePass', general.logUrl, general.isLoged, ctrl.changeUserPass)
app.delete('/:id',      general.logUrl, general.isAdmin, ctrl.remove) 
 
exports.app = functions.region(process.env.REGION).https.onRequest(app) 

(MSG Functions)

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

app.post('/search_by' , general.logUrl,                                 ctrl.searchWithParams   )
app.post('/search'    , general.logUrl,                                 ctrl.search             )
app.get('/my'         , general.logUrl, general.isLoged,                ctrl.getMy           )
//app.get('/'           general.logUrl,, general.isAdmin,               ctrl.get                ) 
app.post('/create'    , general.logUrl, general.isLoged,                ctrl.createNew       ) 
app.put('/'           , general.logUrl, general.isLoged,                ctrl.update             )
app.post('/edit'      , general.logUrl, general.isLoged,                ctrl.update             )
app.delete('/:id'     , general.logUrl, general.isLoged,                ctrl.remove             )
app.get('/comment/:id', general.logUrl, general.isLoged,                ctrl.getComments        )
app.delete('/images'  , general.logUrl, general.isLoged,                ctrl.removeImage        )
app.post('/images'    , general.logUrl, general.isLoged, upload.any() , ctrl.uploadImage        )
app.get('/images'     , general.logUrl, general.isLoged,                ctrl.getImages          )
app.get('/images/:id' , general.logUrl, general.isLoged,                ctrl.getImages          )
app.get('/:id'        , general.logUrl,                                 ctrl.getData            )
app.post('/:id'       , general.logUrl, general.isLoged,                ctrl.comment            )

//app.use('/comment', require('./comment.route')              )

exports.app = functions.region(process.env.REGION).https.onRequest(app)

The problem is that when I make a post to / tas / images, I is answering the function I have for the / user / me.

The first is a POST and the second is a GET, I don't understand what is happening. I leave an example of how I organized, if anyone can advise me or guide.

1
So, it seems like you are receiving the result as if you were consuming another function? You try POST to / tas / images and the result is a GET of user / me? Have you tried to deploy the minimal required with only this functionality? - Joss Baron
I have tried to go up each function little by little; and it seems that when it matches that the methods of the controllers are called in the same way. Or a method which coincides with another another function not really know which is the one to answer. I'm starting to think that this is an error when creating the Express app, which shares the created object between the different functions. Launching this code as a node project instead of functions does not occur this problem. - DoberDog

1 Answers

1
votes

I've been trying different things with respect to this problem and I found the solution, but not very smart and I would like someone to help me respect.

The problem comes from the way I create the ExpressJs app. I have it in an export of a separate module to have the configuration I need, but from what it seems it works as a singleton, so when applying the routes, the last one that corresponds to the method is responding.

The solution was to directly create the object in each of the functions, as I put below.

let corsOpts = (req, res, next) => {
    res.header("Access-Control-Allow-Origin", "*");
    res.setHeader('Access-Control-Allow-Headers', 'Content-Type,Accept,X-ACCESS_TOKEN,Authorization,authorization,x-access-token');
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept,X-ACCESS_TOKEN,Authorization,authorization,x-access-token");
    res.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
    if ('OPTIONS' === req.method) {
        //respond with 200
        console.log("REQ", "OPTIONS");
        res.sendStatus(200);
      } else {
        //move on
        return next(); 
      }
    return res
}
  
app.use(corsOpts);
app.all('*', corsOpts);
app.use(cors());
app.options('*', cors())

//app.use(express.json());
//app.use(express.urlencoded({ extended: false })); 
app.use(express.static(path.join(__dirname, 'public')));  
app.use(bodyParser.json({limit: '50mb'}));
//app.use(bodyParser.urlencoded({limit: '50mb', extended: true}))

app.disable('x-powered-by');    

app.post('/change_password',general.logUrl,                  ctrl.changePasswordAuth )
app.post('/getByToken'     ,general.logUrl,                  ctrl.getByTokenAuth     )
app.post('/register'       ,general.logUrl,                  ctrl.registerAuth       )
app.post('/recovery'       ,general.logUrl,                  ctrl.recoveryAuth       )
app.get('/recovery/:token' ,general.logUrl,                  ctrl.recoveryGetByTokenAuth       )
app.post('/login'          ,general.logUrl,                  ctrl.loginAuth          )
app.get('/list'            ,general.logUrl, auth.isAdmin,    cUser.getUser           ) 
app.get('/me'              ,general.logUrl, auth.isLoged,    cUser.meUser            )
app.get('/status/:id'      ,general.logUrl, auth.isAdmin,    cUser.statusUser        )
app.post('/user_role/:id'  ,general.logUrl, auth.isAdmin,    ctrl.user_roleAuth      )

exports.app = functions.region(process.env.REGION).https.onRequest(app)