can not GET / chat socket.io

0

I'm trying to start the socket.io service, but the error appears Can not GET /

My server.js is like this

var socket  = require( './node_modules/socket.io' );
var express = require('./node_modules/express');
var app     = express();
var server  = require('http').createServer(app);
var io      = socket.listen( server );
var port    = process.env.PORT || 3000;
var users = [];

server.listen(port, function () {
  console.log('Server listening on port %d', port);
});

When I run node server.js it starts the service, but which one try to access the url aparace Can not GET /

    
asked by anonymous 03.06.2016 / 13:28

1 answer

1

I think you are missing the service (URL) you are going to run a certain function on, and when you access, send the file you want to see in the browser:

That is:

...
var users = [];

app.get('/', function(req, res){
    res.sendFile(__dirname + '/chat.html'); // colocar isto de acordo a sua estrutura de diretorios
});

io.on('connection', function(socket){
  socket.on('chat message', function(msg){
    io.emit('chat message', 'isto veio do server: ' +msg);
  });
});

server.listen(port, function () {
  console.log('Server listening on port %d', port);
});

chat.html:

<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.4.6/socket.io.js">

</script>
<script>

var socket = io();

socket.emit('chat message', 'olá');

socket.on('chat message', function(msg){
    alert(msg);
});

</script>

And now go to url ...:3000/

    
03.06.2016 / 13:43