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 Express Basics Using Templates with Express Using Pug in Your Express App

Pug template configuration, how does express know where to look ?

From video https://teamtreehouse.com/library/using-pug-in-your-express-app

app.set('view engine', 'pug');
...
res.render('index')

Based on the app.set('view engine', 'pug') command, how does express know to look into this specific folder views/index.pug ?

And how come when I try to do it manually, res.render('./views/index.pug') or res.render('./views/index'), it doesn't work?

1 Answer

Rohald van Merode
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Rohald van Merode
Treehouse Staff

Hi JASON LEE 👋

When you're setting a view engine with that first line in your code block by default Express will start looking for pug files in your /views directory. Therefor you can omit the path to the file as well as the extension .pug in your render method.

Using the complete filepath should still be working though. If it doesn't you might want to check if you're using the right path. For example if you got those render methods in a separate directory like routes/index.js you'll want to change the path to jump up a directory before moving into the views folder like so: res.render('./../views/index.pug')

I hope this is helpful 🙂

I think i figured out why the separate directory is not working. It has to reference the directory RELATIVE to the views folder. So relative to the views folder it would be res.render('../views/index.pug') or res.render('./index.pug'). In the first example the ../ goes to the parent directory of the views folder and then back to the /views folder