cannot get/ chat socket.io

Asked

Viewed 160 times

0

I am trying to start the socket.io service, but error appears Cannot 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 running Node server.js it starts the service, but which one I try to access the Cannot GET url/

1 answer

1


I think it is missing which service (URL) will perform a certain function, and when access send the file you want to see in the browser:

That is to say:

...
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 the url ...:3000/

  • It worked, but now I can not access the application... only when I put it on the server, accessing localhost/chat, then runs, but it is not in real time...

  • What do you mean? You have a page for example: index.html that you want to see in your browser?

  • I’ll edit according to what I think is necessary

  • That, I have a chat, ta working now, but only when I access it by Wamp, and when I tried to access by localhost:3000 was cannot get, now it does not appear but the error, but the page is blank.

  • give me a few minutes.

  • I edited on top, see if it solves

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.