cloudcmd: /console/console.js 404 when using prefix

Hi, When I set prefix to /cloudcmd and press the console button, devtool shows a 404 for /cloudcmd/console/console.js. The cause is that cloudcmd is prepending prefix to "/console" as prefix for console-io: https://github.com/coderaiser/cloudcmd/blob/v11.5.3/server/cloudcmd.js#L143-L146 When it goes to console-io, https://github.com/cloudcmd/console-io/blob/v9.0.0/server/index.js#L103 it will not work as it seems, because req.url will be "/console/console.js" instead of "/cloudcmd/console/console.js" due to the mounting feature of express. I tried changing prefix + '/console' to '/console' and it works. I didn’t read further in console-io, so I’m not sure if it’ll break things in console-io. Can you take a look?

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 16 (8 by maintainers)

Commits related to this issue

Most upvoted comments

Regarding express mount point, there is app.baseUrl. You are right that client side still need to know the prefix, that’s why I say server side:

You can eliminate most of the prefix tricks in server side source code

The client code doesn’t need change the way they work. They keep prepending the prefix when they request resources, it’s just no longer specified by the user of the middleware. I believe the current way this middleware works has the same effect of mounting in most cases, but there’s still some use cases I can think of where it does not satisfy:

  • If I have a express app which loads modules as sub apps dynamically, it will have a name for each sub app, mount them at /${name}, and expect them to work. It doesn’t know the prefix way.
  • One can mount an app at several paths. It’s as simple as app.use(['/cloudcmd', '/admin'], cloudcmd({..})). The prefix way doesn’t benefit from it.

I’m not familiar with the client code right now. It’s possible that I missed something. 😄

You didn’t reproduce it because you’re running it as a standalone app, by executing cloudcmd --prefix /cloudcmd, or using it as a middleware at the root path. However, as I mentioned above, when you use the mounting feature of express with cloudcmd, this issue comes in, because express strips the baseUrl for you. I highly recommend making cloudcmd work with mounting if you offer its functionality as a middleware, because it’s a very common pattern in express. With it you allow the middleware to work on any path without telling it the prefix. To repro the issue, use the following code:

app.use('/cloudcmd', cloudcmd({
	socket,
	config: { prefix: "/cloudcmd" }
}));

Currently it only works by:

app.use('/', cloudcmd({	//or just omit the 1st argument
	socket,
	config: { prefix: "/cloudcmd" }
}));

I recommend it to be:

app.use('/cloudcmd', cloudcmd({	//or just omit the 1st argument
	socket,
	config: { /* no prefix is needed */ }
}));

It not only makes it easier to use, but also easier to implement, I guess, because you only deal with logical paths within your middleware. You can eliminate most of the prefix tricks in server side source code down the stack(console-io, etc.) I believe. I appreciate it if you can consider my advice.