【问题标题】:Can't find png file at localhost server在 localhost 服务器上找不到 png 文件
【发布时间】:2017-03-10 23:19:16
【问题描述】:

错误代码 http://localhost:3000/public/resource/calendar.png 加载资源失败:服务器响应状态为404(未找到)

我想要网络服务。我不知道为什么会出现这个错误。

它在本地文件中运行良好,但是从nodejs开始这个错误

error

这是我的来源

app.js

var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server);
server.listen(3000);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});

index.html(源代码太长)

这是错误代码

text-align: center;
background: url('./public/resource/calendar.png');
background-size: contain;

且 png 文件路径为 D:\Hackathon\public\resource

和app.js,索引路径是D:\Hackathon

请帮帮我

【问题讨论】:

    标签: html css node.js


    【解决方案1】:

    您需要挂载一个中间件来提供静态文件。

    请看https://expressjs.com/en/starter/static-files.html

    app.use(express.static('<root_folder_for_resources>'));

    【讨论】:

    • @정두진 如果对您有帮助,请采纳我的回答,对其他人也有用!
    • 好的,谢谢你的帮助:D
    猜你喜欢
    • 2020-02-07
    • 2014-03-05
    • 2020-02-14
    • 1970-01-01
    • 1970-01-01
    • 2020-08-07
    • 2015-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多