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

Sub-app not working when mounted on "/" #107

Open
waspeer opened this issue Aug 27, 2019 · 11 comments
Open

Sub-app not working when mounted on "/" #107

waspeer opened this issue Aug 27, 2019 · 11 comments
Labels

Comments

@waspeer
Copy link

waspeer commented Aug 27, 2019

I was trying to mount a sub-app on the root ("/") but it was throwing an error:

/node_modules/polka/index.js:95
		let loop = _ => res.finished || (i < len) && arr[i++](req, res, next);
		                                                     ^

TypeError: arr[(i++)] is not a function

I tracked the error back to the .use() function of polka:

use(base, ...fns) {
		if (typeof base === 'function') {
			this.wares = this.wares.concat(base, fns);
                  // SUB-APP IS NOT HANDLED WELL BECAUSE OF THIS CONDITIONAL:
		} else if (base === '/') { 
			this.wares = this.wares.concat(fns);
		} else {
			base = lead(base);
			fns.forEach(fn => {
				if (fn instanceof Polka) {
					this.apps[base] = fn;
				} else { ... etc ....

I have been trying to find a temporary fix without succes. Or is there another way to go about this?

@lukeed
Copy link
Owner

lukeed commented Aug 28, 2019

Hey, sorry for the delay – on vacation right now 🏖

Mounting via .use('/', ...) is identical to .use(...). That's the same in Express too.

This error, while not a super helpful message on its own, usually means that there's a problem with one of the functions you're trying to assign. Perhaps it's not a function at all.

Can you post a snippet containing the use() block and the functions you're trying to add? Or a reproduction repo would be good too.

Thanks

@waspeer
Copy link
Author

waspeer commented Sep 2, 2019 via email

@lukeed
Copy link
Owner

lukeed commented Sep 2, 2019

Haha everyone is gearing up for the end-of-year sprint. Enjoy!

@lukeed
Copy link
Owner

lukeed commented Sep 23, 2019

Just checking in on this – still an issue?

@waspeer
Copy link
Author

waspeer commented Oct 3, 2019

Hi!
Sorry for being slow. I just ran another test and this still seems to be an issue. This is the code I tested it with:

// INDEX.JS
const polka = require("polka");

const { PORT = 3000 } = process.env;

polka()
  .use(require("./sub"))
  .listen(PORT, err => {
    console.log("Listening!");
  });
// SUB.JS
const polka = require("polka");

module.exports = polka()
  .get("/", (req, res) => {
    res.end("r00t");
  })
  .get("/:name", (req, res) => {
    res.end(`Hello there ${req.params.name}!`);
  });

This time the error is a little different:

/node_modules/polka/index.js:7
	return x.charCodeAt(0) === 47 ? x : ('/' + x);
	         ^

TypeError: x.charCodeAt is not a function

The error dissappears when change .use(require("./sub")) to something like .use("sub", require("./sub")).

Hope this helps!

@matthewrobb
Copy link

I, too, am having this same problem exactly.

@lukeed
Copy link
Owner

lukeed commented Oct 10, 2019

This is fixed in next and I'm not sure if it's worth backporting at this point.

As mentioned, the easy workaround is to use('/', ...) instead, which achieves exactly the same intended result.

@lukeed lukeed added the has fix label Oct 10, 2019
@waspeer
Copy link
Author

waspeer commented Oct 10, 2019

Hi!

For me using use('/', ...) also throws an error. The error I mentioned when I opened the issue. But to elaborate.

// INDEX.JS
const polka = require("polka");

const { PORT = 3000 } = process.env;

polka()
  .use("/", require("./sub"))
  .listen(PORT, err => {
    console.log("Listening!");
  });
// SUB.JS
const polka = require("polka");

module.exports = polka()
  .get("/", (req, res) => {
    res.end("r00t");
  })
  .get("/:name", (req, res) => {
    res.end(`Hello there ${req.params.name}!`);
  });

This will start running and log Listening!, but when I try to visit localhost:3000 I get the following error:

	let loop = _ => res.finished || (i < len) && arr[i++](req, res, next);
		                                                     ^

TypeError: arr[(i++)] is not a function

This is the error I mentioned when I opened the issue.

@RomiC
Copy link

RomiC commented Oct 24, 2019

Faced with the same issue. A small snippet below:

const polka = require('polka');

polka()
  .use('/', polka().use('/', (req, res) => res.end('sub-app response')))
  .listen(3000);

@lukeed
Copy link
Owner

lukeed commented Oct 28, 2019

Hey guys, sorry for the delay.

I took a look at this a few times & have come to the conclusion that it's not worth fixing on the current stable branch. In order to fix it correctly, it requires breaking changes that are already part of the 1.0 work (@next).

I will add complete testing for this (it's not fully implemented, yet) and update this issue once a new release has gone out.

Thanks for you patience :)

lukeed added a commit that referenced this issue Oct 29, 2019
lukeed added a commit that referenced this issue Oct 29, 2019
@lukeed
Copy link
Owner

lukeed commented Oct 29, 2019

Available with polka@1.0.0-next.7 via npm install polka@next 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants