【问题标题】:NodeJS on existing php local site现有 php 本地站点上的 NodeJS
【发布时间】:2017-03-15 00:58:24
【问题描述】:

我有一个基于 PHP / Apache 的网站,我想在 div 区域内插入一个 NodeJs 应用程序,我已经将它显示在 iframe 中,如下所示:

<iframe src="http://example.com:8080"></iframe>

我使用下面的代码尝试了 ProxyPass 和 ProxyPassReverse,但它只重定向到我的 nodeJs 应用程序,而 apache 配置文件中没有我的 PHP 站点 /etc/apache2/sites-available/exemple.conf。

ProxyRequests off

<Proxy *>
  Order deny,allow
  Allow from all
</Proxy>

<Location />
  ProxyPass http://localhost:8080/
  ProxyPassReverse http://localhost:8080/
</Location> 

我怎样才能正确地做到这一点。

【问题讨论】:

    标签: php node.js apache


    【解决方案1】:

    终于不需要特定的服务器配置,从客户端对服务器 url + 端口的 ajax 调用就可以了。

    => server.js 文件

    // Use of socket.IO
    var io = require('socket.io')(8080);
    // Establishes server connection
    io.on('connection', function(socket){
    //send data to client ( real time date exemple )
      setInterval(function(){
        io.emit('date', {'date': new Date()});
      }, 100);
    });
    

    => 使用 css #date ID 在网站的任何位置编写脚本

    jQuery(document).ready(function(){
      // Get base path
      var base_url = window.location.origin;
      // Establishes client connection to server
      jQuery.ajax({
        url : base_url + ':8080/socket.io/socket.io.js', // Socket.IO init on port (8080)
        dataType : 'script', // Turn POSTs into GETs for remote-domain requests with 'script' type
        success : function(){
          // Connection to the server
          var socket = io(base_url + ':8080');
          // Get message defined server side
          socket.on('date', function(data){
            // Print msg into html
            jQuery('#date').text(data.date);
          });
        }
      });
    });
    

    【讨论】:

      猜你喜欢
      • 2016-03-07
      • 1970-01-01
      • 1970-01-01
      • 2013-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-27
      • 1970-01-01
      相关资源
      最近更新 更多