Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
This video covers one solution to the serving static files challenge.
Serve static files with __dirname
Another common way to set up the static
middleware so that Express can serve static files from the 'public' folder (or any folder) located at the root of your application is with Node's __dirname
variable.
__dirname
represents the directory name of the current module.
// Serve static files from the root of the application
app.use( express.static(path.join(__dirname, 'public')) )
express.static
function is relative to the directory from where you launch your node process. If you run the Express app from another directory, itβs safer to use the absolute path of the directory that you want to serve (source).
// Serve static files by routing the static server to '/static'
app.use( '/static', express.static(path.join(__dirname, 'public')) )
Resources
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up