Nodejs不显示图像

我有一个非常简单的nodejs应用,它正在呈现HTML页面。

我的目录结构:

C:\Users\user_id\Desktop\keys\nodejs\node-v12.13.1-win-x64
12/02/2019  05:08 PM    <DIR>          .
12/02/2019  05:08 PM    <DIR>          ..
11/19/2019  01:35 PM            54,162 CHANGELOG.md
12/02/2019  06:23 PM    <DIR>          images
12/02/2019  06:27 PM               353 index.html
10/23/2019  11:31 AM             2,953 install_tools.bat
11/19/2019  01:35 PM            78,655 LICENSE
11/19/2019  01:50 PM        28,787,352 node.exe
10/10/2019  11:31 AM               702 nodevars.bat
10/10/2019  11:31 AM            10,630 node_etw_provider.man
12/02/2019  05:06 PM    <DIR>          node_modules
10/10/2019  11:31 AM               930 npm
10/10/2019  11:31 AM               483 npm.cmd
10/10/2019  11:31 AM               922 npx
10/10/2019  11:31 AM               539 npx.cmd
11/19/2019  01:35 PM            26,931 README.md
12/02/2019  05:07 PM             1,765 server.js
              13 File(s)     28,966,377 bytes
               4 Dir(s)  108,750,237,696 bytes free

html页面

<!doctype HTML>
<html>
  <head>
    <title>
      Index
    </title>
  </head>
  <body>
    <h1>
      <p>Hello! Image
        <br><br>
          <img src="http://localhost:3000/images/img_chania.jpg" alt="Flowers in Chania" width="660" height="345">
      </p>
    </h1>
    <h1>
      Bye Bye! Image
    </h1>
  </body>
</html>

server.js文件

const express = require('express')
const app = express()
const path = require('path')
const port = 3000

/*
* Default route for the web app
*/
app.get('/',(req,res) => res.send('Welcome'))

/*
* Route to render HTML Page
*/
app.get('/renderHTML',res) => {
    res.sendFile('./index.html',{
        root: path.join(__dirname,'./')
    })
})

app.listen(port,() => console.log(`App listening on port ${port}!`))

所有图像都存储在图像目录中。

如果我仅渲染index.html页面,则图像可见。 当我们通过节点应用程序运行图像时,这些图像只是不显示。

这似乎是路径问题,当使用inspect进行调试并单击控制台时,出现404错误。

非常感谢您的帮助

谢谢

wangweimayuanyuan 回答:Nodejs不显示图像

您有一条通往(inside an Activity) boolean win = whatever.checkWin(gameboard,this); 的路线,而您有一条通往/的路线。

您没有通往/renderHTML的路线,因此当然会给您404。

使用the static module as described in the Getting Started guide

/images
,

静态图像必须与静态中间件一起使用。查看本文以了解详细信息。 http://expressjs.com/en/starter/static-files.html#serving-static-files-in-express

app.use(express.static('public'))

本文链接:https://www.f2er.com/2993161.html

大家都在问