8

In my application im using express framework to serve the client side files.But while giving background image for the html element.Its showing failed to load given url.

var express = require('express')
    , http  = require('http');

var app = express();
app.configure(function(){
    app.use(express.static(__dirname + '/public'));
});
var server = http.createServer(app);
var io = require('socket.io').listen(server);
server.listen(8000);

In public folder i have created javascripts,stylesheets,images folder.Now i'm getting javascripts and stylesheets.But i don't know how to access the image file.

.logo {
    background:url('localhost:8080\logo.jpg');//This image url not loading
    float:left;
    width:20px
    height:20px;
}
Nick Mitchinson
  • 5,452
  • 1
  • 25
  • 31
sachin
  • 13,605
  • 14
  • 42
  • 55

2 Answers2

22

If your file directory is like

/public
    /stylesheets
    /javascripts
    /images
        /logo.jpg

then your public access begins at the /public directory. This means that in order to access the image, the address would be localhost:8080/images/logo.jpg.

In summary, you had two problems.

  1. Use a forward slash (/), not a backslash (\) in your URLs
  2. You missed including the image directory in the address
Nick Mitchinson
  • 5,452
  • 1
  • 25
  • 31
  • What if the image is in a private folder? – Lion789 Dec 20 '13 at 06:54
  • While I'm not entirely sure what you mean by private, I am going to assume you mean 'only owner has read access'. In this case you will have to give read access to whichever user is running the node process. You could likely make a new group, add the node user to the group, change the file group owner to the new group, and then give the directory group read access. – Nick Mitchinson Dec 20 '13 at 15:19
3

You are also listening on port 8000, but the image you are referencing has port 8080.

Shane N
  • 599
  • 1
  • 4
  • 14