How to serve image files using express framework in node.js?

In my application, I use the express framework for working with files on the client side. But when providing a background image for an html element. It indicates that the specified URL could not be loaded.

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 the shared folder, I created javascripts, stylesheets, images folder.Now, I get javascripts and stylesheets. But I do not 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; } 
+7
source share
2 answers

If your file directory is similar to

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

then your public access starts in the /public directory. This means that to access the image, the address will be localhost:8080/images/logo.jpg .

So, you have two problems.

  • Use slash ( / ) instead of backslash ( \ ) in your urls
  • You missed including the image directory in the address
+19
source

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

+3
source

All Articles