简体   繁体   中英

Mix Node.js and PHP?

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?

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 complexity without achieving anything.

If you're going to even think about adding this kind of complexity 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 comes with. PHP V8js . I agree however with the other answer that this should be avoided as much as possible.

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