"无法获取/null”;当使用nodejs运行服务器时

"Cannot GET /null" when running a server with nodejs

本文关键字:nodejs 服务器 运行 null quot 获取      更新时间:2023-09-26

编辑#2:嗯,重新启动计算机后,错误消失了。我什么都没改。又是一个美好的日子浪费在一个根本不存在的bug上。。。。

我想你可以删除这个吗?

我的代码以前在没有任何路由的情况下运行良好,但后来这种情况开始发生,我没有像一天那样提交,所以我不想加载上次保存。这意味着什么?我该如何解决?

我真的不知道该问什么,因为当你在谷歌上搜索"cannot get/null nodejs"时,没有什么能和我所经历的类似。如果你想让我发布的话,我有大约500行代码。

_________________________________
<!doctype html>
<html>
<body>
<div id="signDiv">
Username: <input id="signDiv-username" type="text"></input><br>
Password: <input id="signDiv-password" type="password"></input><br>
<button id="signDiv-signIn">Sign in</button>
<button id="signDiv-signUp">Sign up</button> 
</div>
<div id="gameDiv" style="display:none;">
    <canvas id="ctx" width="500" height="500" style="border:1px solid #000000;"></canvas>
    <div id="chat-text" style="width:500px; height:100px; overflow-y:scroll">
        <div>Hello</div>
    </div>
    <form id="chat-form">
        <input id="chat-input" type="text" style="width:500px"></input>
    </form>
</div>
<script src="http://cdn.socket.io/socket.io-1.4.5.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script type="text/javascript">
  var userip;
</script>
<script type="text/javascript" src="https://l2.io/ip.js?var=userip"></script>
<script type="text/javascript">
  document.write("Your IP is :", userip);
</script>

<script>
    var socket = io();
    //sign in/up
    var signDiv = document.getElementById('signDiv');
    var signDivUsername = document.getElementById('signDiv-username');
    var signDivSignIn = document.getElementById('signDiv-signIn');
    var signDivSignUp = document.getElementById('signDiv-signUp');
    var signDivPassword = document.getElementById('signDiv-password');
    signDivSignIn.onclick = function(){
        socket.emit('signIn',{username:signDivUsername.value,password:signDivPassword.value});
    };
    signDivSignUp.onclick = function(){
        socket.emit('signUp',{username:signDivUsername.value,password:signDivPassword.value});
    };
    socket.on('signInResponse', function(data){
        if(data.success){
            signDiv.style.display = 'none';
            gameDiv.style.display = 'inline-block';
        } else
            alert("Sign in unsuccessul.");
    });
    socket.on('signUpResponse', function(data){
        if(data.success){
            alert("Sign up successful.");
        } else
            alert("Sign up unsuccessul.");
    });

    var chatText = document.getElementById("chat-text");
    var chatInput = document.getElementById("chat-input");
    var chatForm = document.getElementById("chat-form");
    var ctx = document.getElementById("ctx").getContext("2d");
    ctx.font = "30px Arial";

    socket.on('newPositions', function (data){
        ctx.clearRect(0,0,500,500);
        for(var i=0 ; i < data.player.length; i++)
            ctx.fillText(data.player[i].number, data.player[i].x, data.player[i].y);        
        for(var i=0 ; i < data.bullet.length; i++)
            ctx.fillRect(data.bullet[i].x-5, data.bullet[i].y-5, 10, 10);       
    });
    // socket.on('hit event', function(linkbulletdata){
    //  window.location=String(linkbulletdata);
    //  signDiv.style.display = 'inline-block';
    //  gameDiv.style.display = 'none';

    // });

    socket.on('addToChat', function(data){
        chatText.innerHTML += '<div>' + data + '</div>';
    });

    socket.on('evalAnswer', function(data){
        console.log(data);
    });

    chatForm.onsubmit = function(e){
        e.preventDefault();
        if(chatInput.value[0] === '/') {
            socket.emit('evalServer', chatInput.value.slice(1));
        }
        else {socket.emit('sendMsgToServerLoadGun', chatInput.value);}
        chatInput.value = '';
    }

    $(document).ready(function(){
        $("body").keydown(function(e){
              switch(e.which) {
                  case 32: //space ATTACK
                  socket.emit('keyPress', {inputId:'attack', state:true});
                  console.log('shoot');
                  break;
                  case 37: // left
                 socket.emit('keyPress', {inputId: 'left', state: true }); 
                  console.log("keycode= " + e.keyCode); 
                  break;
                  case 38: // up
                    socket.emit('keyPress', {inputId: 'up', state: true }); 
                  console.log("keycode= " + e.keyCode); 
                  break;
                  case 39: // right
                  socket.emit('keyPress', {inputId: 'right', state: true }); 
                  console.log("keycode= " + e.keyCode); 
                  break;
                  case 40: // down
                  socket.emit('keyPress', {inputId: 'down', state: true });
                  console.log("keycode= " + e.keyCode); 
                  break;
                  default: return; // exit this handler for other keys
              }
              e.preventDefault(); // prevent the default action (scroll / move caret)

        });
    });
    document.onmousemove = function(event){
        var x = -250 + event.clientX -8;
        var y = -250 + event.clientY -8;
        var angle = Math.atan2(y,x)/Math.PI*180;
        socket.emit('keyPress', {inputId:'mouseAngle', state:angle});
    }
    document.onmousedown = function(event){
        socket.emit('keyPress', {inputId:'attack', state:true});
    }
    document.onmouseup = function(event){
        socket.emit('keyPress', {inputId:'attack', state:false});
    }

    $(document).ready(function(){
        $("body").keyup(function(e){
              switch(e.which) {
                  case 32: //space
                  console.log('space');
                  socket.emit('keyPress', {inputId:'attack', state:false});
                  break;
                  case 37: // left
                 socket.emit('keyPress', {inputId: 'left', state: false }); 
                  console.log("keycode= " + e.keyCode); 
                  break;
                  case 38: // up
                    socket.emit('keyPress', {inputId: 'up', state: false }); 
                  console.log("keycode= " + e.keyCode); 
                  break;
                  case 39: // right
                  socket.emit('keyPress', {inputId: 'right', state: false }); 
                  console.log("keycode= " + e.keyCode); 
                  break;
                  case 40: // down
                  socket.emit('keyPress', {inputId: 'down', state: false }); 
                  console.log("keycode= " + e.keyCode); 
                  break;
                  default: return; // exit this handler for other keys
              }
              e.preventDefault(); // prevent the default action (scroll / move caret)

        });
    });

</script>
</body>
</html>

问题很可能不是node.js服务器代码,而是客户端代码。如果我启动express.js服务器,然后在浏览器中访问localhost:3000/null,我可以重现这个错误。浏览器返回"无法获取/null"。

因此,如果没有任何进一步的信息,问题很可能是客户端代码:从node.js服务器请求信息的代码。最可能的原因是:客户端代码中的一个js对象似乎被设置为null,然后该对象被用作向node.js服务器发出请求的路径。您的服务器可能没有/null的路由,因此您看到了该错误。

希望能有所帮助!如果我完全走错了轨道,请随意发布一些客户端代码。