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

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

Open
jacobwod opened this issue Dec 3, 2020 · 2 comments
Open

Comments

@jacobwod
Copy link
Contributor

jacobwod commented Dec 3, 2020

Currently we use __dirname (which returns the path of server.js) as starting point in finding our way to /public.

const root = path.normalize(`${__dirname}/../..`);

app.use(Express.static(`${root}/public`));

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.

@cdimascio
Copy link
Owner

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

@jacobwod
Copy link
Contributor Author

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.

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

No branches or pull requests

2 participants