Nodejs: page not found

Asked

Viewed 46 times

0

I was making my application, but when I update the page the browser returns me that the page is not available. I’ll post my code so you can tell me if there’s something wrong

JS

var http = require("http").createServer(servidor);
var io = require("socket.io").listen(http);
var fs = require("fs");

var usuarios = 0;

function servidor(req, res){
    res.writeHead(200);
    res.end(fs.readFileSync("index.html"));
}

io.on("connection", function(socket){
    console.log("Usuário Conectado");
    usuarios++;

    io.emit("usuarios online", usuarios);
    socket.on("mensagem", function(msg){
        io.emit("mensagens", msg);
    });


    socket.on("disconnect", function(){
        console.log("Usuario Desconectado");
        usuarios--;
        io.emit("users", usuarios);
    });
});


http.listen(3000);

HTML

<!DOCTYPE html>
<html lang="pt-BR">
<head>
    <meta charset="UTF-8">
    <title>Chat</title>
    <script src="/socket.io/socket.io.js"></script> <!-- chamamos o socket.io que por padrão o socket.io cria a rota http sem precisarmos interferir -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css"> <!-- bootstrap -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script><!-- jquery -->
    <script type="text/javascript">
    var socket = io.connect(); //isso é necessário para fazer a conexão com o socket.io do node.js

    //quando recebemos do node.js "mensagens" utilizando o método append do jquery para exibir as mensagens
    socket.on('mensagens', function(msg){
        $('.container').append($('<div class="alert alert-success">').text(msg));
    });

    //quando recebemos "usuarios online" do node.js
    socket.on("usuarios online", function(total){
        $('.container').append($('<div class="alert alert-info">').text("Novo Usuario entrou, total de " + total + " Usuarios Online"));
    });

    //função que é disparada quando é pressionado o botão
    function enviarMsg(){
        socket.emit("mensagem", $("#inputMsg").val()); //enviamos o valor do input
        $("#inputMsg").val(''); //depois deixamos ele vazio
    }
    </script>
</head>
<body>
    <div class="container">
        <!--Mensagens ficam aqui-->
    </div>

    <div style="background-color:#2c3e50; width:100%; height:50px; position:fixed; bottom:0; padding:9px;" class="row">
        <div class="col-md-10">
            <input type="text" class="form-control col-md-11" id="inputMsg">
        </div>          
        <a onclick="enviarMsg()" class="btn btn-success">Enviar</a>
    </div>
</body>

  • What is the url that you wrote in the browser to test the app?

  • @Sergio, I’m calling him in http://localhost:3000/

  • 1

    I don’t know how it goes when the user who asked the question found the answer, but in case any moderator wants to ask the question, feel free

  • I solved the problem just by adding http://localhost:3000 client, within the function that connects to the server

  • 1

    @Gustavo write down his own solution as an answer and accept it as a solution to this question ;)

  • Thanks for the tip @Sneepsninja

Show 1 more comment

1 answer

0


I solved the problem just by doing this

var socket = io.connect('http:localhost:3000');  
  • has now failed to accept this answer as a solution ;)

Browser other questions tagged

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