套接字节点。js表达404错误

时间:2022-11-29 17:38:51

From a simple chat example at https://github.com/guille/chat-example/blob/master/index.js

来自https://github.com/guille/chat-example/blob/master/index.js的一个简单聊天示例

For information, I'm not serving html file through following code, either the html is going to be embedded inside this iphone app or hosted somewhere. Question is how to connect the html to the nodejs environment where the sockets will be handled or is it a must that the html goes through the res.sendfile statement?

对于信息,我不是通过下面的代码来服务html文件,要么是html将嵌入到这个iphone应用程序中,要么在某处托管。问题是如何将html连接到要处理套接字的nodejs环境,还是html必须通过res.sendfile语句?

    app.get('/', function(req, res){
      res.sendfile(__dirname + '/index.html');
    });

html

html

<script src="js/api/jquery-2.1.1.min.js"></script>
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
</head>

<body>
<script type="text/javascript">
$(document).ready(function(){
    var socket = io();
    socket.emit('chat message', "hello world");
});
</script>

I'm getting a 404 error, attached screenshot 套接字节点。js表达404错误

我得到一个404错误,附加的截图

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
  res.sendfile(__dirname + '/index.html');
});

io.on('connection', function(socket){
  socket.on('chat message', function(msg){
    console.log(msg);
    io.emit('chat message', msg);
  });
});

http.listen(3000, function(){
  console.log('listening on *:3000');
});

1 个解决方案

#1


0  

You are using res.sendfile when it should be res.sendFile (capital F)

当它应该是res.sendfile时,您正在使用res.sendfile(大写F)

#1


0  

You are using res.sendfile when it should be res.sendFile (capital F)

当它应该是res.sendfile时,您正在使用res.sendfile(大写F)