6

I have images in /public folder and I want to show them..simply like this: <img src="a.jpg">

My app.js code

var express = require('express') , app = express() , http = require('http') , server = http.createServer(app) , io = require('socket.io').listen(server); server.listen(8080); app.use('/public', express.static(__dirname + "/public")); 

If I open it in localhost this error is still showing

NetworkError: 404 Not Found - http://localhost:8080/public/a.jpg" 
7
  • 1
    I tested your code and it's working fine. Commented Mar 9, 2013 at 10:51
  • hmm really? So why it is not working on my localhost? Commented Mar 9, 2013 at 10:52
  • Are you sure that your express app is pointing to the right public folder? Commented Mar 9, 2013 at 10:54
  • yes, but I don't know what was that..now it works, but I didn't changed any code..and 1 minute ago there was same code and it didn't worked.. thanks for helping me Commented Mar 9, 2013 at 10:56
  • Yes it's strange. You're welcome anyways. Commented Mar 9, 2013 at 10:57

4 Answers 4

4

in your case it is enough to write:

app.use(express.static('public')); 

it will serve directly the public folder.

an image in this path /public/images/somePhoto.png would be shown like that: http://localhost:8080/images/somePhoto.png

source: https://expressjs.com/en/starter/static-files.html

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

Comments

1

You need to drop the /public/ bit from your URL to your image.
So it becomes just http://localhost:8080/a.jpg

5 Comments

I disagree! app.use('/public', express.static(__dirname + "/public")); should serve files with /public pathname prefix.
uf now I don't understand what I am doing bad
@fardjad i disagree with your disagreement! expressjs.com/faq.html - a request to "GET /javascripts/jquery.js" would first check "./public/javascripts/jquery.js"
@alexjamesbrown as it says in How can I prefix a pathname for serving statics?, it exposes the req.url without the prefix (passed as the first argument) to express.static middleware.
@alexjamesbrown In addition to my previous comment: that doesn't mean that the request URL shouldn't have /public prefix in this case.
0

handling static files in express You simply need to pass the name of the directory where you keep your static assets, to the express.static middleware to start serving the files directly. For example, if you keep your assests in a folder called public, you can use app.use(express.static('public')) as follows, my images are in public\images

var express = require('express'); var app = express(); app.use(express.static('public')); app.get('/', function (req, res) { res.send('Hello World'); }) var server = app.listen(8081, function () { var host = server.address().address var port = server.address().port console.log("Example app listening at http://%s:%s", host, port) }) 

save the code above to server.js

$ node server.js

Now open http://127.0.0.1:8081/images/logo.png in any browser and image will show up

Comments

-1

I removed the backslash before the "public".

In my app.js or server.js, I changed this:

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

to this:

app.use(express.static('public')); 

Just in case anyone still has this issue.

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.