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

Send data to display on html page (express, mongo)

Hello, Right now I'm learning how to use MongoDB along with Express. As I've seen, you can pass a variable to a .pug file in order to display that value on the page. I'm using html files and I was wondering how I can achieve the same effect.

Thanks!

In order to send variables to a page in Express, you have to render the page using a templating engine (like Pug for example). You can send an html file using the express sendFile command, but you can't attach any variables to it.

I would either rename your html files to pug files (since valid html is valid pug shorthand) or convert your html files to pug files.

2 Answers

Are pug templates the convetional way for websites these days? Are there any other ways to display data retrieved from mongo on a page?

Using a template engine is the only way that I know of, but there are several different template engines to choose from:

https://expressjs.com/en/guide/using-template-engines.html

Pug just happens to be a popular one (I'm a fan of it myself.)