Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

what's the rule for express.static('public') ? absolute or relative path?

Not sure the the logic here

I look up the doc http://expressjs.com/en/starter/static-files.html

it says "Pass the name of the directory that contains the static assets to the express.static middleware function to start serving the files directly. "

"the path that you provide to the express.static function is relative to the directory from where you launch your node process"

Does that mean

  1. if I run node src/app.js => I use express.static('public') 2.if I run node app.js in the src folder=>I use express.static('../public')

1 Answer

Testing it out, yes running src/app.js from your project's root or app.js from src/ makes a difference in what path to use. But it seems best to assume you're running relative to your project's root because programs like nodemon, etc. will run from there, grabbing the script to run from the package.json file.

Thanks !

How about using

__dirname

seems safer not to break down the app in any situation