【问题标题】:images not showing when using node js with handlebars(.hbs)使用带有把手(.hbs)的节点js时不显示图像
【发布时间】:2021-05-23 13:15:34
【问题描述】:

我试图在这里寻找答案,但到目前为止没有任何效果,关于车把的话题也不多。 我正在努力让图像显示在我的节点应用程序上。

我在 app.js 上有这个,下面是我试图让图像显示在 .hbs 文件中的代码:

const express = require("express");
const path = require("path");

const hbs = require("hbs");
const app = express();
const bodyParser = require("body-parser");
const port = process.env.PORT || 80;

require("./db/conn"); 

const SwimmingCollection = require("./models/schema"); 
const static_path = path.join(__dirname, "../public");
const template_path = path.join(__dirname, "../templates/views"); 
const partials_path = path.join(__dirname, "../templates/partials"); 

app.use(express.static(static_path));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(bodyParser.urlencoded({ extended: true }));
app.set("view engine", "hbs");
app.set("views", template_path); 

hbs.registerPartials(partials_path); 

app.get("/", (req, res) => {
res.render("index");
});

app.listen(port, () => {
   console.log(`Listening to the port at ${port}`);
});

如何解决这个问题?

【问题讨论】:

  • 您的图片在您的公共文件夹中吗?
  • 是的,在公共文件夹中我创建了图像文件夹。所有图像都在图像文件夹内
  • 我认为您应该显示请求图片的 URL。

标签: html css node.js express handlebars.js


【解决方案1】:

试试下面的静态路径。

app.use(express.static(path.join(__dirname, 'public')))

https://expressjs.com/en/starter/static-files.html

【讨论】:

    猜你喜欢
    • 2020-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多