-1

I have looked at a lot of similar questions but I can't get it to work, so I'll just ask it myself

I have a folder like this:

Source Code
  Frondend
    Graphs.html
    Graphs.css
    Temperature.js
  Server_Backend
    server.js

I want to run these file with node.js, I can also use express if necessary.

I have this in server.js to load my html:

fs.readFile('../Frondend/Graphs.html', function (err, html) {

    if (err) throw err;

    http.createServer(function(request, response) {
        response.writeHeader(200, {"Content-Type": "text/html"});
        response.write(html);
        response.end();
    }).listen(8080);
});

these are the links in my html header:

<script type="text/javascript" src="Temperature.js"></script>
<link rel="stylesheet" type="text/css" href="Graphs.css">

How do I also include my css and js file? As I said I tried a lot of things already but I can't seem to get it to work.

0

2 Answers 2

0

So what the browser will do, after you send the html (which you do perfectly), it will look for http://yourdomain.com/Temperature.js and http://yourdomain.com/Graphs.css. To allow express to expose these to the browser, express has the express.static function.

You can do, when using express, is

app.use(express.static('./Frondend'));

This will take any request to your website, and will check if there is a file in the Frondend folder, and if so, will send that file.

Hope that makes sense :)

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

1 Comment

It's not working but there might be other things going wrong idk, is it possible that it doesn't work because it is in a different folder? From Server_Backend it first needs to go back to Source code because there is the Frondend folder.
0

since you are a beginner, I would use express.js. It makes it much easier to deal with things like static assets (css and javascript files) and helps you with routing requests to different code parts (like /users, /files, /about)

To solve your question, best would be to look at following information from express.js about static files after you looked at the getting started guide

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.