Azura Co
Azura Co

Reputation: 13

NodeJS res.sendFile not sending images or directorys

so as said above, i run the html file only and i'm able to see the pictures, but when i run the file via node it sends me a 404 error on the file i used.

<html>
<body>
<img src="aaa.jpg" />

<script src="/socket.io/socket.io.js">
</script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script>
</script>
</body>

</html>

and the node code

var app = require("express")();
var http = require("http").Server(app);
var io = require("socket.io")(http);
app.get("/", function (req, res) {
    res.sendFile(__dirname + "/Movie.html");
});
http.listen(5969, function () {
    console.log("Server Open");
});

how can i stop this from happening?

Upvotes: 1

Views: 2907

Answers (1)

Bidhan
Bidhan

Reputation: 10697

To serve static files such as images and scripts, you need to include a middleware that serves static files. Put all your static files inside of a public directory and serve them like this.

var express = require("express");
var app = express();
var http = require("http").Server(app);
var io = require("socket.io")(http);
// Serve static files
app.use(express.static(__dirname + '/public'));
app.get("/", function (req, res) {
    res.sendFile(__dirname + "/Movie.html");
});
http.listen(5969, function () {
    console.log("Server Open");
});

Upvotes: 1

Related Questions