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 Build a Simple Dynamic Site with Node.js Creating a Basic Template Engine in Node.js Making Our Views DRY

Gabriel Ducharme
Gabriel Ducharme
11,948 Points

Why put the CSS in the HTML markup instead of external?

How can we access it externally using node?

5 Answers

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

Hey there,

You can take this project further by creating a folder /public and have node check the url of the request and use the fs module to see if that file exists in the public folder if it does write the file contents to the response.

This is what a framework like Express does out of the box though.

This course was just getting you familiar with what something like a framework was doing under the hood so it's less magic to you.

Eventually, when you deploy a node app, you'd use something like Apache or nginx to serve your public folder so your node app doesn't have to deal with those requests but in many development environments use node to serve content from their public/static folder.

Regards Andrew

Awesome!

Thanks Andrew!

That's cool, the problem is you should have only 1 minified compressed css file or the operation will get so tedious and boring on big projects :( I have a question, You said we can leave this stuff to APACHE or NGINX, Really ? but why we're using node in the first place ? And if we did should we disable some features on APACHE like HTTP SERVER because we gonna create our own server with NodeJs !

Edit:

Your browser normally gets linked files through port 8080. Since we've set up the server to listen on a different port, Node.js isn't going to be able to serve those files. There are ways around this issue, but this example has the style embedded in the head for simplicity.

Good Question. Andrew Chalkley Maybe you can provide some insight?

Gary Calhoun
Gary Calhoun
10,317 Points

I think from what I gather you can think of it like an include well it is an include its including chunks of code to be dynamically generated by node.js server since we are creating a site which basically swaps out information it gets so there is no need to link the css separately because each dynamic page is getting the css from the header.html file. As mentioned before this css is not alot of code but if it became too much and you find yourself scrolling then its better to do what Andrew suggested to keep it separate.

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

I don't know much about Node as yet but there are situations where you might want to include your CSS as inline styles in your HTML page if maybe there's not a lot of CSS you're using or you're using CSS for a specific purpose. It shouldn't effect any external CSS too much as inline styles override external styles. :-)