【问题标题】:Node JS Socket.io节点 JS Socket.io
【发布时间】:2014-11-27 18:42:18
【问题描述】:

我一直在努力设置 socket.io npm 模块,并且我的浏览器能够与后端 NodeJS 脚本交互。但我不确定我哪里出错了。我记得以前在控制台中看到过这样的消息:

“Socket.io .. 已启动 ..”

但现在什么也没有发生。下面是我的 NodeJS 代码:

var express = require('express');
var server = require('http').createServer(express);
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
global.socket = require('socket.io')(server);
//global.socket = require('socket.io').listen(80, {log:true});
//socket.listen(80, {log:true});

在我的 index.js 文件中,我有一个捕获 socket.io 传入连接的函数。当我从浏览器尝试以下 URL 时:

http://localhost:3000/socket.io/socket.io.js

我收到一条错误消息:

Error: Failed to lookup view "error" in views directory "C:\amr\public" at Function.app.render  

但是当我请求以下网址时:

http://localhost/socket.io/socket.io.js

浏览器上好像渲染了socket.io.js文件:

!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.io=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){

我不确定如何从浏览器或后端查看我的网络套接字是否正常工作,我必须在我的 HTML 页面中包含什么脚本 URL 才能使连接正常工作?

<script src="http://localhost:80/socket.io/socket.io.js"></script>

【问题讨论】:

    标签: node.js socket.io


    【解决方案1】:

    您可能没有向公众公开 socket.io 客户端库。如果您使用 express 生成器,只需将该文件复制并粘贴到 yourproject/public 并将您的请求发送到 localhost:3000/&lt;insert socket.io clientside filename&gt;

    如果您没有使用 express 生成器,请创建一个名为 public 的文件夹,然后在您的主项目文件中放置 app.use(express.static(__dirname + '/public')); 行,然后执行我上面描述的操作

    哦,您的错误是指视图引擎无法找到用于错误打印的视图。修复它,它会为您提供更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-22
      • 1970-01-01
      • 2015-05-20
      • 1970-01-01
      • 2014-05-13
      • 2020-10-28
      • 2013-04-07
      • 1970-01-01
      相关资源
      最近更新 更多