Error: Cannot GET using Express & handlebars

Error: Cannot GET using Express & handlebars

我是 JS/Handlebars 的新手,我无法在 VS Code 中显示我的 home.hbs 文件中的图像。 当我 运行 服务器时,我得到这个:

这是我的服务器代码:

const express = require('express');
const app = express();
const port = 3000;



app.set('views', 'views');
app.set('view engine', 'hbs');
app.use(express.static('public'));

app.get('/', function(req, res) {
    res.render('home', {});
});

app.listen(port);
console.log('server listening on port 3000');

我的“home.hbs”代码:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Handlebars at Work</title>
</head>
<body>
    <img src="/public/img/logo.png">
    <h1>Hello There, {{name}}</h1>
</body>
</html>

“GET”错误明确指出

Cannot GET /public/img/logo.png

这就是我的全部信息,如有任何帮助,我们将不胜感激。

指定静态文件处理程序的路径前缀。变化:

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

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

并另外验证您确实有一个名为 public/img/logo.png.

的文件