【问题标题】:NodeJs socket io file not found未找到 NodeJs 套接字 io 文件
【发布时间】:2019-03-05 18:58:02
【问题描述】:

我想在 web 服务器上使用 socket io。当我在 localhost 上工作时,一切正常。我在 plesk 面板上安装了文件。节点模块安装成功。当我调用 index.html 时,我得到一个错误是 http://example.com:3000/socket.io/socket.io.js not found。我的错在哪里?

index.html

<!DOCTYPE html>
<html ng-app="socket" ng-controller="homeController">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<script src="http://example.com:3000/socket.io/socket.io.js"></script>
<body>
    <div class="container">
        <table class="table table-striped">
            <thead>
                <tr>
                    <th>id</th>
                    <th>Status</th>
                    <th>Category</th>
                </tr>
            </thead>
            <tbody>
                <tr ng-repeat="i in results">
                    <td>{{$index}}</td>
                    <td>{{i.status}}</td>
                    <td>{{i.category}}</td>
                </tr>
            </tbody>
        </table>
    </div>
</body>
<script>
    var app = angular.module('socket', []);
    app.controller('homeController', function ($scope) {
        var socket = io.connect('http://example.com:3000');
        socket.on('connected', function (data) {
            socket.emit('ready for data', {});
        });

        socket.on('update', function (data) {           
            $scope.results = data;
            if (!$scope.$$phase) $scope.$apply();
        });  
    });
</script>
</html>

index.js

const Express = require('express');
const Request = require('request');
const http = require('http');
const db = require('./lib/database');
const app = Express();
const server = http.createServer(app);

var io = require('socket.io').listen(server);

io.sockets.on('connection', function (socket) {
    socket.emit('connected', { connected: true });
    socket.on('ready for data', function (data) {
     db.on('notification', function(title) {
         const sql = "select * from public.test";
         db.query(sql, (err, result) => {
          if (err) res.status(err.code).json({code: err.code, message: err.message}).end();
          socket.emit('update', result.rows);
         })
     });
    });
});

server.listen(3000);

【问题讨论】:

    标签: node.js socket.io plesk


    【解决方案1】:

    1- express.static

    如果您想提供静态文件,如 JavaScript、CSS、Image 等,您可以使用 express.static 内置中间件函数。

    函数签名是:

    express.static(root, [options])
    

    root 参数指定提供静态资产的根目录。有关 options 参数的更多信息,请参阅 express.static。

    例如,使用以下代码在名为 public 的目录中提供图像、CSS 文件和 JavaScript 文件:

    app.use(express.static('public'))
    

    现在,您可以加载公共目录中的文件了:

    http://localhost:3000/images/kitten.jpg
    http://localhost:3000/css/style.css
    http://localhost:3000/js/app.js
    http://localhost:3000/images/bg.png
    http://localhost:3000/hello.html
    

    2-CDN 或者,您可以将 CDN 用于 socket.io.js 或 jquery、bootstrap 等其他库。

    您可以从这里获得 CDN socket.io cdn

    【讨论】:

      猜你喜欢
      • 2017-10-14
      • 2016-08-27
      • 2021-12-27
      • 2019-02-20
      • 2021-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-02
      相关资源
      最近更新 更多