php - node.js 和 reactjs 作为 php 模板渲染服务是个好主意吗?

标签 php node.js reactjs

我的网站是用 PHP 编写的。 Node.js 也适用于 socket.io。我一直在考虑使用 Reactjs 进行服务器端渲染,这样我就可以重用 View 。计划使用 v8js 扩展,但后来我遇到了 this article谈论两种可能的解决方案:

1.使用v8js扩展和React-PHP-V8Js

2.从php发送数据到node.js渲染 View ,示例:

Node .js

require("babel/register");  
var React = require('react');  
var express = require('express');  
var path = require('path');  
var bodyParser = require('body-parser');

var app = express();  
app.use(bodyParser.json());  
app.use('/', function(req, res) {  
    try {
        var view = path.resolve('./views/' + req.query.module);
        var component = require(view);
        var props = req.body || null;
        res.status(200).send(
            React.renderToString(
                React.createElement(component, props)
            )
        );
    } catch (err) {
        res.status(500).send(err.message);
    }
});

PHP

use GuzzleHttp\Client;

$app->get('/{name:.*?}', function($name) use ($app) {
    $client = new Client(['base_url' => 'http://localhost:3000']);
    $response = $client->post('/', [
        'json' => ['name' => ucfirst($name ?: 'World')],
        'query' => ['module' => 'hello'],
    ]);
    $contents = $response->getBody()->getContents();

    return response($contents, 200);
});

作者说他更喜欢第二种方法,因为第一种方法:

Unless you're comfortable installing (and updating) libv8 and the V8Js PECL extension on your production machines, this is not an option. Personally, I wouldn't go this route. Installing the dependencies is cumbersome, dependency management is tricky, and there aren't many resources to guide you along the way. In addition, you'll need to account for the fact that your javascript builds should not be bundled with react if you want to re-use them.

你能告诉我他所说的“javascript 构建不能与 react 捆绑在一起”是什么意思吗?我一直在考虑使用 v8js + React-PHP-V8Js 所以我想了解它的一些缺点。另外,您更喜欢第二种方法(node.js 作为模板渲染服务)吗?我已经运行了 node.js,所以这似乎是个好建议。

最佳答案

这是一个意见问题,所以你得到的任何答案都是一个意见。

我会说不,这不是一个好主意,并解释我的理由。

每个软件项目都有一个复杂性,可以粗略地用使用的流行语数量来衡量。也就是说,“一个由 AWS Lambda 服务支持的高可用性、多语言、React 网站”听起来很毛茸茸。每个流行语都会使之前流行语的努力成倍增加。您建议构建“一个使用 PHP 使用 ReactJS 进行服务器端渲染的站点,并使用底层 Node V8JS 渲染……”并且在遇到问题域之前就已经耗尽了复杂性。

你会遇到 React 具有完全不同的渲染模型的问题。你会遇到缓存问题。您将遇到系统管理员问题。

不要这样做。

关于php - node.js 和 reactjs 作为 php 模板渲染服务是个好主意吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38258479/

相关文章:

javascript - 如何在更改 JSON 文件后将其重新加载为模块

javascript - Webpack "devtool eval"在 devtools 中没有显示正确的文件内容

php - 0000-00-00 00 :00:00 in timestamp

PHP APC 不工作

PHP PDOStatement : fetch a row, 作为第一列作为数组的键

javascript - MongoDB-Stitch。填充方法或类似方法?

php - .htaccess 中的重写规则 : what does it do

javascript - 与 lodash 的显式链接不应用快捷融合

javascript - ReactJs 如果 div 包含 className 将负 tabindex 设置为 childs

javascript - 在 React 中动态添加后 getElementById