Express Routing Error when using Photon Webhooks on Parse.com



I have made a photon app and put on

https://groun***eakers.parseapp.com/ as baseURL on photon webhook, PathCreate put as GameCreate. Then I created a game using Photon which succeeded. Then in my Parse.com, I deploy the cloud code provided by Photon:
  var hookName = "GameCreate";

    var fail = require('cloud/util.js').fail(hookName);
    var ok = require('cloud/util.js').ok(hookName);


    var db = require('cloud/db.js');

    exports.register = function(app){

    console.log("does this work?");
app.post('/' + hookName, function(req, res) {

    console.log(hookName + ": before req.query = " + JSON.stringify(req.query));
    console.log(hookName + ": before req.body = " + JSON.stringify(req.body));

    var ctx = {hookName: hookName, ret: {ResultCode: 0, Mesage: ""}, res: res, fail: fail, ok: true};

    console.log(hookName + ": req.query = " + JSON.stringify(req.query));
    console.log(hookName + ": req.body = " + JSON.stringify(req.body));
The "does this work?" is displayed but not the later console.logs. Here is what Parse.com log said:

I2015-09-17T15:09:32.151Z]does this work?
I2015-09-17T15:09:32.176Z]Express Routing Error: Cannot POST //GameCreate
Could you help me with this? Why is it said "Express Routing Error"?

Comments

  • JohnTube
    JohnTube mod
    edited September 2015
    Hi @thuyquai,

    From the logged error it is clear that the relative path is interpreted as "//GameCreate" instead of "/GameCreate".

    Please try removing one [forward] slash ('/'). You have two options:
    - change the base URL from https://groundbreakers.parseapp.com/ to https://groundbreakers.parseapp.com.
    - change app.post('/' + hookName, function(req, res) to app.post(hookName, function(req, res).

    This is not a Photon related issue btw. A little bit of Googling does no harm. On the contrary you could learn new cool things.
  • thanks John :). I have google for a day before posting the question. i'll try as you suggested. I did try the second option though, will test the first option now. I'll report back if it works.
  • Report:
    Hi John, I have removed the '/' and now Parse's log said
    2015-09-20T09:30:59.894Z]v59 Ran custom endpoint with:
    Input: {"method":"POST","url":"/GameCreate","headers":{"accept":"application/json","content-length":"364","content-type":"application/json","host":"groundbreakers.parseapp.com","version":"HTTP/1.1","x-forwarded-for":"10.252.5.60","x-forwarded-proto":"https","x-http-method-override":"POST"}}
    Result: Success

    I2015-09-20T09:31:00.272Z]Express Routing Error: Cannot POST /GameCreate
  • Hi @thuyquai,

    Did you modify the default 'app.js' file?
    You have a problem somewhere in that file that causes a '/GameCreate' 404 not found.
    You should have this line require('cloud/GameCreate.js').register(app); before app.listen();.
  • Hi john,
    I had that line as you suggested

    photo cloud_zpsbm27wdqo.png

    I could test with groundbreakers.parseapp.com/hello from browser. When I tried to change post to get from GameCreate.js I could also access it from browser If I remember correctly. I changed it back to post though.
  • JohnTube
    JohnTube mod
    edited September 2015
    You have a problem with 'GameCreate.js' file. Please use the original one from the github repo.
    I tried all other webhooks and they work fine.
    Maybe you forgot to change app.post(hookName, function(req, res) back to app.post('/' + hookName, function(req, res).

    Hamza
  • Hey man, you are absolutely right, it absolutely work :smile: .
    So my fault was:
    1. I put https://groundbreakers.parseapp.com/ instead of https://groundbreakers.parseapp.com and it result in the //GameCreate error.
    2. Then I change the app.post('/' + hookName to app.post(hookName and still the same error
    3. I then changed https://groundbreakers.parseapp.com/ to https://groundbreakers.parseapp.com which is a right move but still keep app.post(hookName thus result in the Can not post /GameCreate error.
    4. After that I follow your final tip and revert the GameCreate file back to it's origional state and it works!!!

    Thanks for your help, I really appreciated this :)