generator-express-no-stress icon indicating copy to clipboard operation
generator-express-no-stress copied to clipboard

Change how we figure out which directory should be served as static

Open jacobwod opened this issue 4 years ago • 2 comments

Currently we use __dirname (which returns the path of server.js) as starting point in finding our way to /public. https://github.com/cdimascio/generator-express-no-stress/blob/53082f95d018be189a5e0e4ae3f903e77cdaefb1/app/templates/server/common/server.js#L17

https://github.com/cdimascio/generator-express-no-stress/blob/53082f95d018be189a5e0e4ae3f903e77cdaefb1/app/templates/server/common/server.js#L30

There are two issues with this scenario:

  1. It doesn't work after we've compiled the source (in the resulting /dist, /public is just one directory up from server.js, not two)
  2. The path is not OS-agnostic

I propose a solution that uses process.cwd() instead. Given that we launch the process from /, it's an easy task to find /public:

app.use(Express.static(path.join(process.cwd(), "public")));

This works well both with in dev and production and is OS-agnostic as we compile the path using path.join(). Furthermore, in combination with #61, the const root can be entirely removed as it's not used anymore.

Let me know if I should prepare the PR for this one too.

jacobwod avatar Dec 03 '20 09:12 jacobwod

@jacobwod this sounds reasonable. Are you up for submitting a PR?

cdimascio avatar Dec 20 '20 05:12 cdimascio

Absolutely. But you are aware of that the implication of this is that the process must be launched from a directory that actually contains public?

jw@abc ~ % node test.js 
process.cwd() is /Users/jw
jw@abc ~ % cd dev 
jw@abc dev % node ../test.js 
process.cwd() is /Users/jw/dev

I think it's better, given how this generator is constructed (and I've implemented the change in my project as I found it more useful) - but it does changes the current behaviour.

jacobwod avatar Dec 21 '20 09:12 jacobwod