javascript - Mix Node.js and PHP? - Stack Overflow

Could be possible to generate a page with PHP and then run Node.js to apply some edits to it?On Googl

Could be possible to generate a page with PHP and then run Node.js to apply some edits to it?

On Google I've found examples of PHP and Node.js integration but they are always two different files which work together with Ajax or similar things.

My goal instead is to run Node.js (or something which can run javascript) in this way:

$html = "<html>
            <body>PHP
                <script>document.body.innerHTML = \"Javascript\";</script>
            </body>
         </html>";

$html = run_js($html);

Is it possible?

Could be possible to generate a page with PHP and then run Node.js to apply some edits to it?

On Google I've found examples of PHP and Node.js integration but they are always two different files which work together with Ajax or similar things.

My goal instead is to run Node.js (or something which can run javascript) in this way:

$html = "<html>
            <body>PHP
                <script>document.body.innerHTML = \"Javascript\";</script>
            </body>
         </html>";

$html = run_js($html);

Is it possible?

Share asked Sep 12, 2013 at 9:51 Fez VrastaFez Vrasta 14.9k24 gold badges109 silver badges176 bronze badges 13
  • 1 Please explain why would you do that? – moka Commented Sep 12, 2013 at 9:56
  • Technically, yes, but I would not advise it. What are you trying to do? – cbednarski Commented Sep 12, 2013 at 9:57
  • I get a page from a remote server and I've an about 2k rows of JS which parses and edit it, and I'd like to move the action to server side without rewrite the entire script. – Fez Vrasta Commented Sep 12, 2013 at 9:58
  • And how is PHP supposed to talk to node if not via AJAX? It's the same as thinking PHP speaks to MySQL over invisible wire instead of TCP/IP and network. You can have that run_js function, but it will involve some kind of HTTP request from PHP to node. – N.B. Commented Sep 12, 2013 at 10:03
  • 1 Technically, this isn't such bad of a question but it lacks a lot of context. There are many people who try to put PHP and JS together for some reason, and had this question been asked right from the start - you might get some proper answers. However for your purposes - do you even need PHP in this case? Node does everything PHP can, why mix both if you can use only Node to achieve all of it? – N.B. Commented Sep 12, 2013 at 10:26
 |  Show 8 more ments

2 Answers 2

Reset to default 7

No. Do not do this. It may be possible, but it's a really bad idea. Pick a single technology stack for your project and stick to it.

Splitting you project over multiple technology stacks does nothing of any value (whatever you're wanting Node to do could be done in PHP, and vice-versa), so all it really does is add a massive amount of plexity without achieving anything.

If you're going to even think about adding this kind of plexity to your system, you need to understand the implications: This will kill your performance; it will create a maintenance and debugging nightmare; it will probably introduce weird, hard-to-find bugs.

In short, I really, really advise you not to do this.

If you want to use Node.js, that's fine; use it. But don't mix it into a PHP system; start a new project from scratch.

If you really really have to do this you can use PHP V8js. The built-in javascript engine that PHP es with. PHP V8js. I agree however with the other answer that this should be avoided as much as possible.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744924257a4601331.html

相关推荐

  • javascript - Mix Node.js and PHP? - Stack Overflow

    Could be possible to generate a page with PHP and then run Node.js to apply some edits to it?On Googl

    2天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信