julian
julian

Reputation: 4714

node.js - can't load my files with express3

I am using express3 and when I'm trying to load my html/javascript/css files it loads the html without the css.

I'm using this code to load the files -

express.get('/', function (req, res) {
    res.sendfile("index.html");
    res.sendfile("style.css");
    res.sendfile("script.js");
});

So what can I do?

Upvotes: 1

Views: 803

Answers (2)

Hacknightly
Hacknightly

Reputation: 5144

First, link the css and javascript to the page via tags in the header of your HTML

<link rel='stylesheet' href='/path/to/style.css' />
<script src='/path/to/javascript.js'></script>

Be sure that you're using the static middleware in your app.use() section, this will tell Express where to find your static files.

app.use(express.static(__dirname + '/public'));

Now, your Express app should serve your static css and js files.

Upvotes: 2

Benoir
Benoir

Reputation: 1244

You link to css and javascript from the html page...

Try looking at the static files example app in the express github repo: https://github.com/visionmedia/express/tree/master/examples/static-files

Basically you make another http request for static files (.js, .css)

Upvotes: 0

Related Questions