【问题标题】:Images not showing pug, express node.js图片不显示哈巴狗,表达 node.js
【发布时间】:2020-06-19 15:36:51
【问题描述】:

我已经完成了设置图像以显示在我的应用程序中的操作:

http://www.tutorialsteacher.com/nodejs/serving-static-files-in-nodejs

我有当前目录结构:

rootproject
-public
--images
---image.jpg
server.js

server.js:

// require all dependencies
var express = require('express');
var app = express();
var PythonShell = require('python-shell');
var path = require('path');

// set up the template engine
app.set('views', './views');
app.set('view engine', 'pug');

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

index.pug:

html
    head
        title= title
    body
        img(src='/static/images/image.jpg' , alt='some image') 
        h1= message

但这不起作用,并在控制台中显示:GET http://localhost:3000/static/images/image.jpg 404 (Not Found)

【问题讨论】:

    标签: node.js express pug


    【解决方案1】:

    您的静态路由的设置方式,您应该参考 /public 中的图像,而不是 /static。

    img(src='/public/images/image.jpg' , alt='some image') 
    

    【讨论】:

      【解决方案2】:

      公共目录中不存在路径/static/images/image.jpg,您必须删除/static,它应该可以工作:

      img(src='/images/image.jpg' , alt='some image') 
      

      【讨论】:

      • 在服务器 js 中他们使用了 app.use("/public", express.static(path.join(__dirname, 'public')));而不是 app.use(express.static(path.join(__dirname, 'public')));所以,它应该以 /public 开头。
      【解决方案3】:

      就我而言,在我的 server.js 文件中,它有

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

      而不是像上面的问题

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

      所以,以 /public 开头的图像路径对我不起作用。相反,我使用了 images/image.jpg。它对我有用。

      【讨论】:

        猜你喜欢
        • 2021-06-07
        • 1970-01-01
        • 2017-06-30
        • 1970-01-01
        • 1970-01-01
        • 2018-10-20
        • 1970-01-01
        • 1970-01-01
        • 2020-04-17
        相关资源
        最近更新 更多