不能在哈巴狗上显示图像

时间:2017-02-12 19:47:07

标签: javascript html css node.js express

这是我的代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Home Page</title>
</head>
<body>
<img src="resources/mainlogo.png" style="width:304px;height:228px;">
<h1>Welcome to our user management application</h1>
<h3>Please see links below to navigate our homepage</h3>

<a href = /adduser>Create new user</a>
<a href = /users>List users</a>

</body>
</html>

除了图像

之外的所有

这是我的文件结构:

index.html
resources
  mainlogo.png

我做错了什么?

这是渲染代码

app.get('/', function (req, res) {

    // render main page
    res.sendFile(__dirname + "/index.html");


});

这也是来自控制台的错误

Error http://localhost:8000/resources/mainlogo.png 404 (Not Found)

尽管图像位于资源文件夹内。

3 个答案:

答案 0 :(得分:1)

您需要通过app.use(express.static('resources')); //This will allow express to access any file in that folder 访问资源文件夹。这应该可以解决问题:

...
<img src="mainlogo.png" style="width:304px;height:228px;">
...

HTML:

.controller('mynew',['$scope', '$firebaseArray', function($scope, $firebaseArray) {
    var ref = firebase.database('https://myappurl.firebaseio.com/contacts').ref();
    $scope.contacts = $firebaseArray(ref);
    console.log($scope.contacts);
}]);

答案 1 :(得分:0)

添加此

app.use(express.static('resources'))

并更改此

<img src="mainlogo.png" style="width:304px;height:228px;">

帮助

答案 2 :(得分:0)

您需要使用express.static中间件来提供静态文件,例如图像和CSS文件。

在你的情况下,这应该有效:

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

查看here

的静态文档