yuAriellexi 2019-10-19 09:37 采纳率: 100%
浏览 468
已结题

nodeJS,web-server文件路径问题

小白求问,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')
})
  • 写回答

2条回答 默认 最新

  • dabocaiqq 2019-10-19 09:39
    关注
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?