未显示pug的图像,express node.js
我已经介绍了如何设置要在应用程序中显示的图像: 我有当前的目录结构:未显示pug的图像,express node.js,node.js,express,pug,Node.js,Express,Pug,我已经介绍了如何设置要在应用程序中显示的图像: 我有当前的目录结构: 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
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 404(未找到)按照静态路由的设置方式,您应该在/public而不是/static中引用图像
img(src='/public/images/image.jpg' , alt='some image')
公共目录中不存在路径
/static/images/image.jpg
,您必须删除/static
,它应该可以工作:
img(src='/images/image.jpg' , alt='some image')
在我的例子中,在我的server.js文件中 app.use(express.static(path.join(uu dirname,'public')) 而不是像上面的问题一样 app.use(“/public”,express.static(path.join('public'))
因此,该图像路径以/public开始,不适用于我。相反,我使用了images/image.jpg。在ServerJS中,他们使用app.use(“/public”,express.static(path.join(u dirname,'public'));而不是app.use(express.static(path.join(uu dirname,'public'));因此,它应该从/public开始。