【问题标题】:GET http://localhost:3000/salah.jpg 404 (Not Found) error on consoleGET http://localhost:3000/salah.jpg 控制台上出现 404(未找到)错误
【发布时间】:2020-04-15 13:03:18
【问题描述】:

我是 node.js 的新手,只想从 app.js 文件访问我的 home.ejs 文件。 它没有加载我在 home.ejs 上的图像。只是加载内容。

我的 app.js 代码。

var express = require("express")

var app = express();

app.get("/", function(req, res){
    res.render("home.ejs");
})

app.listen(3000, function(){
    console.log("Server Started!")
})

现在是我的 home.ejs 代码。

<h1>hello</h1>

<img src="salah.jpg">

现在是文件结构。

在 startwithEjs 文件夹中。 节点模块 视图文件夹 app.js 文件 杰森文件。

在视图文件夹中。 主页.ejs 萨拉赫.jpg

【问题讨论】:

  • 在处理文件时,您可能需要考虑使用 multer。

标签: node.js get localhost http-status-code-404 ejs


【解决方案1】:

您没有提供访问图像的方法。尝试添加类似的内容:

app.get("/salah.jpg", function(req, res){
  res.writeHead(200, {'Content-Type': 'image/jpeg'});
  res.end("salah.jpg");
})

最终,您将需要编写如下代码:图像文件夹并提供到每个图像的路线。或者您可以查看serve-static middleware

这里是了解更多关于how to use the response object的链接。

【讨论】:

    猜你喜欢
    • 2021-09-14
    • 1970-01-01
    • 2018-11-22
    • 2021-02-22
    • 2021-06-21
    • 2019-07-24
    • 1970-01-01
    • 2021-05-19
    • 2019-12-11
    相关资源
    最近更新 更多