admin管理员组

文章数量:1332881

I'm using Express and for some reason, when using JavaScript I can access images but whenever I try and directly implement the image route to the src such as

<img src="images/background.png">

The localhost determines that it is unable to get the image.

I'm pointing a static to a /public route in the server.js file which is why it is baffling me as to why it can receive some images but not all?

This is my current line that determines the public route.

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

I'm using Express and for some reason, when using JavaScript I can access images but whenever I try and directly implement the image route to the src such as

<img src="images/background.png">

The localhost determines that it is unable to get the image.

I'm pointing a static to a /public route in the server.js file which is why it is baffling me as to why it can receive some images but not all?

This is my current line that determines the public route.

app.use(express.static('public'));
Share Improve this question edited Mar 15, 2017 at 21:46 lin 18.4k4 gold badges65 silver badges87 bronze badges asked Mar 15, 2017 at 21:34 cmiotkcmiotk 3152 gold badges3 silver badges10 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 6

Try a absolute path definition:

var path = require('path');
app.use(express.static(path.join(__dirname, 'public')));

Your directroy structure should look like this:

 server.js -> "where app.use(express.static()) is defined"
 ---| public
 -------| images
 -----------| background.png

I couldnt GET my png because there were some invisible characters in the name of the file. Make sure there are no special characters in the file name, you can try renaming it.

本文标签: javascriptExpress Cannot GET imagesStack Overflow