twitter-bootstrap 无法在本地主机上获取图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32646651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 23:10:25  来源:igfitidea点击:

Cannot get image on localhost

node.jsimagetwitter-bootstrapexpress

提问by Deepna

I am working on a Node.js Express project and have used Bootstrap carousal template in index.ejs page. The image tag is not working if I try to get the image saved inside the project under "views" folder.

我正在处理一个 Node.js Express 项目,并且在 index.ejs 页面中使用了 Bootstrap carousal 模板。如果我尝试将图像保存在“views”文件夹下的项目中,则图像标签不起作用。

 <img  class="img-circle" src= "/proj1.png"  />  

The index.ejs file is also in "views" folder. The error is: GET http://localhost:3000/proj1.png404 (Not Found). I have tried changing the relative path of the file also but it did not work.

index.ejs 文件也在“views”文件夹中。错误是:GET http://localhost:3000/proj1.png404(未找到)。我也尝试更改文件的相对路径,但没有奏效。

回答by DevAlien

YOu have to serve your static files from express:

你必须从 express 提供你的静态文件:

try Adding this:

尝试添加这个:

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

(publicis the folder where your images and everything (assets) reside)

public是您的图像和所有内容(资产)所在的文件夹)

More infos here: http://expressjs.com/starter/static-files.html

更多信息在这里:http: //expressjs.com/starter/static-files.html

回答by Swaraj Giri

You need to tell express from where should it serve static content.

您需要告诉 express 应该从哪里提供静态内容。

app.use(express.static('/path/to/your/folder')).

app.use(express.static('/path/to/your/folder')).

Source: http://expressjs.com/starter/static-files.html

来源:http: //expressjs.com/starter/static-files.html

In production, it's a general convention to serve static files using nginx. Look it up.

在生产中,使用 nginx 提供静态文件是一个通用的约定。查一下。