정두진 정두진 - 1 month ago 8
Node.js Question

Can't find png file at localhost server

Error code
http://localhost:3000/public/resource/calendar.png Failed to load resource: the server responded with a status of 404 (Not Found)

I want web service. and I don't know why this error.

it works well in local file, but start with nodejs this error

error

And Here is my source

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 (source too long)

this is error code

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


and png file path is D:\Hackathon\public\resource

and app.js, index path is D:\Hackathon

Help me plz

Answer

You need to mount a middleware to serve static files.

Please see https://expressjs.com/en/starter/static-files.html

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