Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is node.js with reactjs as php template rendering service a good idea?

Tags:

My website is written in PHP. Node.js is also set up for socket.io. I have been thinking about server-side rendering with Reactjs so I can reuse the views. Planning to use v8js extension, but then I came across this article talking about two possible solutions:

1.Use v8js extension and React-PHP-V8Js

2.Send data from php to node.js to render views, example:

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);
});

The author says he would prefer the second method because the first one:

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.

Could you tell me what he means by "javascript builds not being able to bundle with react"? I have been thinking about using v8js + React-PHP-V8Js so I would like to understand some of the drawbacks of it. Also, would you prefer the second method (node.js as template rendering service)? I have node.js running already so this seems to be a good suggestion.

like image 456
RedGiant Avatar asked Jul 08 '16 03:07

RedGiant


People also ask

Which is the best for next project PHP vs NodeJS?

Winner: Node. js wins the Node. js vs PHP performance battle as it offers better speed and a seamless and concurrent experience both for the developer and end-user.

Can PHP and node js work together?

You can run node and PHP on same server, and even on the same port. The key is to use a server like nginx in front listening on port 80, set up PHP in Nginx as you normally would (using php-fpm) and set up your Node instance to listen locally on some high port like 8081.

When should we use node js over PHP?

High Speed and Consistent Callback from Servers- Web applications developed using Node. js tend to perform better while consistently sending requests to the server. Its asynchronous architecture allows non-blocking execution which is great for any project that needs speed.

Is PHP more secure than NodeJS?

Node. js is fast and lightweight. It is more secure than PHP.


1 Answers

This is an opinion question, so any answer you get will be an opinion.

I'll say no, it's not a good idea, and explain my reasoning.

Every software project has a complexity that can be roughly measured by the number of buzzwords used. That is, "a high-availability, multi-lingual, React website backed by AWS Lambda services" sounds inherently hairy. Each buzzword multiplies the efforts of the buzzwords before. You are suggesting building "a site using PHP using ReactJS for server-side rendering with an underlying node V8JS rendering..." and you run out of complexity before you hit the problem domain.

You will hit issues with React having a totally different rendering model. You will hit caching issues. You will hit sysadmin issues.

Don't do it.

like image 64
Charles Merriam Avatar answered Oct 03 '22 14:10

Charles Merriam