I'm developing an application and the server is currently set up and working well.
This is the index.html that shows when you access the server:
<!doctype html>
<html>
<head>
<title>Socket.IO chat</title>
</head>
<body>
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script type="text/javascript" src="./client.js"></script>
<script type="text/javascript" src="./main.js"></script>
</body>
</html>
However I get a 404 error "Failed to load resource: the server responded with a status of 404 (Not Found)" when importing those javascript files, which are in the same directory.
Here is my server 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+'/index.html'); // DO I NEED TO SEND THE OTHER FILES HERE ASWELL?
});
io.on('connection', function(socket){
console.log('a user connected');
socket.on('disconnect', function(){
console.log('user disconnected');
});
socket.on('chat message', function(msg){
console.log('message: ' + msg);
io.emit('chat message', msg);
});
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
Thanks!
If the HTML file is located in the same path as the scripts are, then use relative path like
src = "main.js"
. If it still doesn't work, maybe it's Express' fault.Add the codes below into your
app.js
:And the scripts can be accessed from the path relative to
/scripts
- e.g.main.js
fromhttp://yourdomain/script.js
but not fromhttp://yourdomain/scripts/main.js
. Sorry for my English.