2

So in node.js I render the html page which is included in view/index.html. I also included a css stylesheet in the head tags, but it's not rendering. Is there anything I should know about how this should be formatted in the file system: for example, does there have to be a style folder or something for the css to be in?

Thanks.

3
  • Rendering with node.js? Just to be clear: don't you mean just serving with node.js, and looking at it in a browser? Commented Mar 9, 2013 at 23:06
  • Yes that's what I mean Commented Mar 9, 2013 at 23:06
  • Feel free to mark a response as accepted. Help keep the site clean by not leaving questions open. Commented Mar 11, 2013 at 21:03

3 Answers 3

5

You will need an endpoint that serves the CSS file, or use a static directory. If you are using Express, the following code when configuring the app will set a directory which will be served statically.

app.use(express.static(path.join(__dirname, 'public')));

This will tell Express to serve everything in the '/public' directory as static content.

If you are not using Express, you can take a look at this question, Node.js static file server logic (using Connect middleware), which should help you.

Sign up to request clarification or add additional context in comments.

2 Comments

app.use(express.static(path.join(__dirname, 'public'))); ^ ReferenceError: path is not defined Getting this error, can you please explain ?
You need to require the path module before you can use it: var path = require('path');
0

You need to write code to actual respond to the HTTP GET request the browser will issue for the stylesheet and send back the CSS data. Basically however you are serving view/index.html it will be similar but with a different URL, different content-type header, and different static file to send in the response. You posted no code, so you get English instead of JavaScript.

Comments

0

What are you using to serve the page at view/index.html? Are you simply using fs to read in the file and then sending the contents of the file in response to the request? Or are you using a middleware framework like Connect or Express? Can you provide a link to the site that you are working on, and the relevant extracts of your JavaScript code?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.