html - Node.js( express ): How to include correct value for 'src' attribute of an 'img' tag?

标签 html css node.js express


我有一张图片,我想将其显示为主页的背景。这就是我正在做的,

 background: url(../../imgs/b2.jpeg) no-repeat center fixed;

图像位于“imgs”文件夹中,导航正确。现在,我正在监听本地主机地址的端口 5000(127.0.0.1) 当我预览我的应用程序时,图像由于以下错误而无法加载,

http://127.0.0.1:3000/imgs/b1.jpeg - Failed to load resource: the server responded with a status of 404 (Not Found)

这是因为浏览器正在查找与 IP 地址相关的文件。在这种情况下,我如何在本地保存的图像中指定正确的位置?

谢谢!

最佳答案

这就是我最终解决它的方法,我设置了我的应用程序来为公共(public)目录提供服务,

app.use(express.static(__dirname + "/public"));

之后,这就是我指定存储在 public/imgs

中的图像路径的方式 localhost:3000/imgs/b1.jpeg

瞧,它成功了!

关于html - Node.js( express ): How to include correct value for 'src' attribute of an 'img' tag?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38731066/

相关文章:

html - 内容不在一个圆 div 中

node.js - nodejs可以安装在免费的虚拟主机上吗

javascript - 没有 sudo 无法运行 npm install。将权限设置为 .npm 不起作用

javascript - 如何使用 JavaScript 验证多个 mcq 问题?

jquery - Google+1 在另一行而不是与其他社交图标内联。有任何想法吗?

javascript - 将 4 个函数调用堆叠在一起

html - 水平对齐一行中的所有 HTML 元素

javascript - 背景图像上带有文本的响应式菜单

html - <a> 和 <button> 的区别

javascript - 如何使用 Javascript 将具有相同名称的对象分组?