NodeJS, Socket.Io and Express
Posted: Fri Jul 28, 2017 5:49 am
I'm trying to get this up and running and the NodeJS seems to be fine, my problem is my HTML page can't find the socket.io/socket.io.js
Here is my server code:
and my html
Now here is my file structure:
project-folder/index.js
project-folder/package.json
project-folder/node_modules
project-folder/public/index.html
When I run the index.js the server seems fine and no issues, when I open the index.html page in the browser on the pi it can't find the socket.io.js file and because of that io is undefined.
Can anyone please offer some help?
Here is my server code:
Code: Select all
var express = require('express');
var app = express();
var server = require('socket.io');
app.use(express.static('public'));
server.createServer(app).listen(8080);
var serv_io = io.listen(server);
serv_io.sockets.on('connection', function(socket) {
console.log('user connected');
socket.emit('SocketCallback', {value:'Connection Successful'});
});
Code: Select all
<head>
<script src="/socket.io/socket.io.js"></script>
</head>
<body>
<script type="text/javascript">
var socket = io();
socket.on('SocketCallback', function (data) {
console.log(data);
});
</script>
</body>
project-folder/index.js
project-folder/package.json
project-folder/node_modules
project-folder/public/index.html
When I run the index.js the server seems fine and no issues, when I open the index.html page in the browser on the pi it can't find the socket.io.js file and because of that io is undefined.
Can anyone please offer some help?