小白求问,about文件中的图片无法显示
文件路径如下:
src
---app.js
public
---css
------style.css
---img
------robot.png
templates
---about.hbs
---help.hbs
---index.hbs
about.hbs文件代码:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="/css/style.css">
</head>
<body>
<h1>{{title}}</h1>
<img src="../public/img/robot.png"><!--- 注意文件路径名,否则图片无法显示---->
<p>Created by {{name}}</p>
</body>
</html>
在app.js中
const path = require('path')
const express = require('express')
const hbs = require('hbs');
const app = express()
const publicDirectoryPath = path.join(__dirname, '../public')
const viewsPath = path.join(__dirname, '../templates')
app.set('view engine', 'hbs')
app.use(express.static(publicDirectoryPath)) //静态文件
//app.set('views', publicDirectoryPath)//动态文件
app.set('views', viewsPath)
app.get('', (req, res) => {
res.render('index', {
title: 'Weather App',
name: 'Andrew Mead'
})
})
app.get('/about', (req, res) => {
res.render('about', {
title: 'About Me',
name: 'Andrew Mead'
})
})
app.get('/help', (req, res) => {
res.render('help', {
title: 'Help',
helpText: 'Helpful Text here'
})
})
app.listen(3000, () => {
console.log('server is up on port 3000')
})