0

I have the following code server side code:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);


app.get('/', function(req, res){
    res.sendfile('index.html');
});
app.get('/data/cursor.png', function(res, res) {
    res.type('png');
    res.sendfile('data/cursor.png');
});

and on the client side i have the following code:

<img class="cursor" src="data/cursor.png">

and when i load the index.html i get the following error: GET http://localhost:3000/cursor.png 404 (Not Found) why is this happening? i also send some other files like the javascript file and css. and they work just fine... so res.sendfile('js/drawingV2.js'); works totally fine.

app.get('/js/drawingV2.js', function(res, res) {
    res.sendfile('js/drawingV2.js');
});

could someone explain to me what i am doing wrong? if you need more code or if i am unclear pls let me know :)

FutureCake
  • 2,614
  • 3
  • 27
  • 70

1 Answers1

1

You are only sending your user the index.html file, or that's how Express is seeing it. You need to send the entire directory with all the files to the user, which is what express.static does.

Let's assume you have a directory, public, that has index.html in it, and a folder called data, with cursor.png in it.

server.js

public -

    index.html

    data -

        cursor.png

And then -

var express = require('express');
var app = express()
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.use(express.static("public")) // serve the whole directory

And viola! It should work. (express.static serves an entire directory)

Flarp
  • 139
  • 1
  • 10