dongyutan1703 2012-07-24 15:29
浏览 155
已采纳

如何将Node.js服务器连接到Apache服务器?

I'd like to use Socket.io and Node.js to do push notifications. The end goal is to do something similar to what Stackoverflow does with their commenting to notify people of new comments.

My site is in PHP and runs on Apache on an EC2 instance. I've heard that Apache doesn't handle concurrency well so I'm interested in using Node to handle the hopefully large # of simultaneous, persistent connections. I guess there are two solutions:

  1. Put Node on same instance as Apache and proxy the two servers
  2. Put Node on a separate instance

In either case, I don't know how the connection between these two servers looks programmatically. For example, while WebSockets/Node handles the sending/receiving of messages, I need to store these messages in my MySQL database and that would require some PHP code, yes/no? Also, how is the message received on my PHP page? Via a $.post to a url like this, http://mysite/receiver.php that would be resolved by my Apache server?

I'd be happy to see either comments or code to help me understand this better.

  • 写回答

3条回答 默认 最新

  • duandun2136 2012-07-24 16:01
    关注
    • You can run different servers at different ports.
    • In order for the servers to communicate in a way you can use Sessions/MySQL/flat files/Cache/json/xml...
    • You can store messages wherever you want there is no limitation on that, MySQL module for node.js exists, memcache module for node.js exists and many more, PHP code is not required, its optional to query the database.
    • The messages on your PHP page are fetched from your database and rendered, if node.js stores stuff in mysql server you can fetch them with PHP or temporarily store to an array and push messages back to client while you store into database for new users to have "current" content.
    • Avoid using ajax since you can stream data to client via websockets in realtime use when needed.

    -- Edited
    Some additional information on How to use vhosts alongside node-http-proxy? link provided from Timothy Strimple at comments , thanks

    Some MySQL drivers can be found in this question What MySQL drivers are available for node.js?

    About configuring ports:
    -On apache you need to manually edit httpd.conf to define default port
    -On node.js things are simpler and you can define a port in code Using node.js as a simple web server

       var connect = require('connect');
        connect.createServer(
         // .. code
        ).listen(PORT);
    

    hope it helped

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献