areyoufried
areyoufried

Reputation: 51

Assets for ExpressJS

ExpressJS noob here. How do I call images stored in /public/images/ directory to my /public/stylesheets/styles.css

The following does not seem to work

.home-bg {
    background-image:url('../images/bg.jpg');
}

Upvotes: 0

Views: 262

Answers (2)

rnrneverdies
rnrneverdies

Reputation: 15627

I guess you are using express like this sample:

app.js

// config
app.configure(function() {
    //..

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

    //..
}

Explanation

express.static, is responsible for serving the static assets and the argument refers to the root directory from which the static assets are to be served.

It does not refer to the path on your web page. refers to the directory in nodejs project. So we add __dirname, then the folder is relative to app.js

In summary:

if, __dirname + '/public' becomes in your http://www.example.com/ then /public/images becomes http://www.example.com/images

So CSS should be:

.home-bg {
    background-image:url('/images/bg.jpg');
}

Upvotes: 1

dkran
dkran

Reputation: 284

are you simply missing quotes on your url?, possible css error?

.home-bg {
    background-image:url('../images/bg.jpg');
}

edit: heh, SO even formats it cute.

Upvotes: 0

Related Questions