简体   繁体   中英

How to pass data from a node.js server to Apache served PHP page?

I am trying to build a node.js chat feature for an existing PHP project. I am using node.js socket.io for it. Node.js is listening to port 3000. Now the problem is when I try to access the chat PHP page http://www.example.com:3000/app/chat.php the page gets downloaded instead of being served. I know that node.js does not serves PHP pages but what workaround can be done for the same?

Node.js

var app = require('express')(),
        server = require('http').createServer(app),
        io = require('socket.io').listen(server),
        httpProxy = require('http-proxy');

server.listen(3000);

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

io.sockets.on('connection', function(socket) {
        socket.on('send data',function (data) {
                io.sockets.emit('latest data',data);           
        });
});

PHP

<?php

// Blah Blah

<script>
                        jQuery(function($))
                        {
                                var socket = io.connect();
                                var $editArea = $('#editAreaID');

                                $editArea.keydown(function(){
                                        socket.emit('send data', $editArea.val());
                                });

                                socket.on('latest data', function(data){
                                        $editArea.val(data);
                                });
                        }
</script>

// Blah Blah

?>

If your PHP page is being downloaded instead of running normally then it has probably nothing to do with the node.js server.

Verify first that PHP is installed correctly and that the PHP module is enabled in the apache configuration.

See https://serverfault.com/questions/215455/what-causes-php-pages-to-consistently-download-instead-of-running-normally

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM