使用哈巴狗时快递无法提供静态文件

问题描述 投票:0回答:1

我正在尝试使用express和pug作为模板引擎来提供静态文件,但不知何故未加载我的资产

我的文件路径:

+front
  +views
   +login
      index.pug
     +images
     +js
     +css
     ...
server.js

这是我的服务器代码:

app.use(express.static('front')); 
app.set('views', './front/views')
app.set('view engine', 'pug')

router.get('/login', (req, res) => {
  res.render('./Login_v1/login', { title: 'Login', message: 'Login'})    
})

pug代码:

  link(rel='icon' type='image/png' href='/images/icons/favicon.ico')
node.js express pug
1个回答
0
投票
首先将路径添加到app.js / server.js(您的express的入口文件)中的静态文件夹中

app.use(express.static(__dirname + 'images/icons'));

您可以通过以下方式访问它

href='localhost:serverPort/favicon.ico

© www.soinside.com 2019 - 2024. All rights reserved.