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

Brian Patterson
Brian Patterson
19,588 Points

Rendering JavaScript, CSS and Html in NodeJS

Can anyone point me to any videos on how to render the above in NodeJS . Or example of how you would go about doing this.

2 Answers

Hey! There is a course on treehouse on how to use express.js with node.js.

Express.js is a tool used with node.js to make websites.

Node.js is used to do stuff on the server-side, or the "backend". It handles stuff that happens behind the scenes, such as user authentication and other stuff. It does not handle rendering html sites.

Express.js can be used with node.js to render html sites. It is basically used so that node.js can do the backend stuff, and express.js can do the website stuff such as rendering a file in the browser when someone types in "/home".

The course on treehouse teaches how to use express.js. You can still use javascript and css just like you would, but they do not use html.

They use something called a templating engine called "Pug" or "Jade" this compiles into html, but it is a little more flexible. Like you can render certain things on the page if a user is logged in or not without javascript.

You can use html with express.js too, but if you are looking to do server-side development, I would not recommend that.

If you already have html files ready, you can use this html to jade convertor to easily convert the code into jade, and then just paste it into the jade file.

If you are looking to do this, I would recommend to take this treehouse course instead of doing it alone.

I hope this helps!

Thanks! :)

Steven Parker
Steven Parker
229,732 Points

Render it where? The whole point of node.js is that it provides a JavaScript run-time engine outside of any browser. So there's no HTML or CSS componenets, nor is there any "document" or "page".

If your JavaScript code works with HTML and CSS it should be executed in a browser instead of node.js.

Brian Patterson
Brian Patterson
19,588 Points

I want to upload the file to Heroku and it will not work in the current file structure. It needs to be run from a server. So that is why I am using node.

Steven Parker
Steven Parker
229,732 Points

Running on a server is exactly what node.js is good for. But there's nothing to render to on the server. The server application needs to deliver the HTML and CSS (and perhaps a separate JavaScript part) to a browser to be rendered.

If you take a closer look at what your application does, you should be able to identify which parts of it belong in the server and which parts should be sent to a browser.