Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subapps are not mounted #16

Merged
merged 2 commits into from Feb 20, 2015
Merged

Subapps are not mounted #16

merged 2 commits into from Feb 20, 2015

Conversation

winniehell
Copy link
Contributor

When I access req.app.mountpath in the Router of a subapp, the behavior is different than from without streamline.

Without express-streamline I get

$ npm test
> curl localhost:3456/subapp/
/subapp
/subapp

but with streamline I get

$ npm test
> curl localhost:3456/subapp/
/
/subapp

Edit by @aseemk: diff between branches

@aseemk aseemk added the bug label Feb 19, 2015
@aseemk
Copy link
Owner

aseemk commented Feb 19, 2015

Thanks for the bug report and thorough example! Yes, I see, you're right.

I'm not sure off the top of my head what would cause this to not work, but I need to take a detailed look at Express 4 in general.

Express routers are valid middleware functions, according to this:

http://expressjs.com/4x/api.html#app.use

So the assumptions express-streamline makes should be valid. But in case they're not, I have a hunch that the issue is somewhere here:

https://github.com/aseemk/express-streamline/blob/0.5.0/index.js#L111-L118

If that's the case, one thing you could try as a workaround for now is to pass a second function to your app.use call, just a dummy middleware handler that does nothing.

Does that fix it for you? If so, that'd be a clue for fixing this issue at the root.

Thanks again for the report and thorough example! When I get to this, I'll make a test case from it.

winniehell added a commit to winniehell-sandboxes/express-sandbox that referenced this pull request Feb 19, 2015
@winniehell
Copy link
Contributor Author

I just started using Express which makes it hard for me to follow your explanation. 👽
Can you help me understanding what you mean by

pass a second function to your app.use call, just a dummy middleware handler that does nothing

My current workaround is to manually set the mountpath... 😃

@winniehell
Copy link
Contributor Author

I simplified the example apps a bit and made it (not) work for Express 3. Same issue there.

@aseemk
Copy link
Owner

aseemk commented Feb 19, 2015

Oh, nice — didn't realize even that simple workaround was possible. Great to hear. =)

And sweet, thanks for the repro on Express 3 too. That's helpful.

winniehell added a commit to winniehell-sandboxes/express-sandbox that referenced this pull request Feb 19, 2015
@winniehell
Copy link
Contributor Author

I think I understand the problem now but I don't know how to fix it:

For

var subapp = express();
var app = express();
app.use('/subapp', subapp);

the condition at this point is true for subapp (because of this). Therefore subapp gets wrapped and app.use is called with the wrapper instead of subapp directly. This then causes app.use to think that subapp is no Express app because of this condition and setting the mountpath is skipped. A similar check is done for Express 2 and Express 3.

@winniehell
Copy link
Contributor Author

How about adding the "Am I an Express app"-condition (lastArg.handle && lastArg.set) here?

@winniehell winniehell changed the title Wrong mountpath for subapps Subapps are not mounted Feb 20, 2015
@aseemk
Copy link
Owner

aseemk commented Feb 20, 2015

Oh wow, I missed that this was a pull request and not an issue. Thanks for submitting!

I don't have time right now to review and test it, but I hope to soon!

@aseemk
Copy link
Owner

aseemk commented Feb 20, 2015

Actually, I see that this is pretty straightforward. Nice work!

It generally LGTM if we can confirm that the tests pass across all versions of Express.

To that end, could you rebase this to the latest master so we know?

# add this repo as an upstream remote:
git remote add upstream https://github.com/aseemk/express-streamline.git

# on your branch, rebase to the current upstream master;
# you can optionally choose to squash some commits together:
git rebase --interactive upstream/master

# ensure that tests still pass, i.e. something didn't break:
npm test

# if at any point, you feel something broke, you can always abort via:
# git rebase --abort

# assuming everything is good, push the latest;
# the `+` is required since this has "rewritten" your branch's history;
# this also assumes your fork's remote is named `origin`:
git push origin +head:patch-1

If any of this is too complex or daunting for you, no worries at all. Just let me know and I'd be happy to test this myself.

Thank you again!

}

return true;
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: let's move this helper function out of patch(), since it doesn't need to be re-defined for every verb.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • done

winniehell and others added 2 commits February 20, 2015 21:56
Prevent `patch` function from wrapping Express apps because they are otherwise no longer recognized by Express as such.
@winniehell
Copy link
Contributor Author

@aseemk Thank you for the review and all the helpful comments! All makes sense to me and I changed it.

For all my forks I actually have two remotes, so it was basically a git fetch --all && git rebase github/master 😉

I pushed the changes in two steps such that there are two builds confirming that the patch targets the issue:
https://travis-ci.org/aseemk/express-streamline/builds/51565117
https://travis-ci.org/aseemk/express-streamline/builds/51567097

P.S. I recently learned that you can convert issues to pull requests... maybe that explains where this pull request suddenly came from 😀 Unfortunately, you can't change the branch (name) of a pull request afterwards.

@aseemk
Copy link
Owner

aseemk commented Feb 20, 2015

Excellent work! Thank you for the thoroughness and attention to detail. =) Merging!

P.S. Ah, got it! Thanks for explaining. =D

aseemk added a commit that referenced this pull request Feb 20, 2015
@aseemk aseemk merged commit 3ca378a into aseemk:master Feb 20, 2015
@winniehell winniehell deleted the patch-1 branch February 20, 2015 23:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants