Do I really need nodeJS on the ENV interface?

I am new to reaction. I want to start a small welcome example of my own.

Most tutorials offer something like this:

app.js

var React = require('react'); var ReactDOM = require('react-dom'); var reactElement = React.createElement('h1', { className: 'header' }, 'This is React'); ReactDOM.render(reactElement, document.getElementById('react- application')); 

index.html

 <!doctype html> <html lang="en"> <head> <meta charset="utf-8" /> <meta http-equiv="x-ua-compatible" content="ie=edge, chrome=1" /> <title>Snapterest</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/ bootstrap/3.3.5/css/bootstrap.min.css"> </head> <body> <div id="react-application"> I am about to learn the essentials of React.js. </div> <script src="./app.js"></script> </body> </html> 

The problem is that this example requires nodeJS (for the requeir () part) and npm install and npm start .. all this.

I can do it differently without nodeJS like this

app.js

 var reactElement = React.createElement('h1', { className: 'header' }, 'This is React'); ReactDOM.render(reactElement, document.getElementById('react-application')); 

index.html

 <!doctype html> <html lang="en"> <head> <meta charset="utf-8" /> <meta http-equiv="x-ua-compatible" content="ie=edge, chrome=1" /> <title>Snapterest</title> <script src=" /react-0.14.8.min.js"></script> <script src=" /react-dom-0.14.8.min.js"></script> </head> <body> <div id="react-application"> dsf </div> <script src="./app.js"></script> </body> </html> 

in this example, I use cdn to import reaction dependencies that nodejs should import in the npm installation phase. the question is which is better? can i use only cdn and not use nodejs entriely? Is it more correct to have nodejs and npm modules (or bower ..) to respond to them?

thanks

+7
javascript reactjs
source share
1 answer

The answer to the question posed in the header is no, you do not need node.js to use React on the client side .

In fact, the second example you give does exactly that: using client side React without mentioning node.js.

However, there are several ways to use node.js that are very useful when building React-based applications.

Use the node.js build tool, such as browserify or webpack , to associate your client code with the neat, neat package that you then serve for the client.

For example, in a project I'm working on, I use a browser to create a single Javascript file stored in public/js/bundle.js , which is included through the plain old <script> in my index.html , and all this is served by Apache. This kit contains my application code (including some React components) along with all the dependencies for my application code (including, but not limited to, react and react-dom ). The main advantage for the client is to reduce the number of requests per page and the necessary bandwidth (since I can use UglifyJS to minimize the whole package). The main advantage of the developer is that you can use tools like Babel to write .jsx code instead of plain old Javascript - this is a huge performance boost.

Write HTTP server in node.js

In recent years, there has been a lot of noise related to the creation of the entire application with node.js - the client side and the server side. The main advantage here is code reuse: imagine you wrote a library that does something interesting with dates. If you write your client-side code in Javascript and your server-side code in PHP, you will have to rewrite this library; if you use node.js on both sides, you need to do it once.

Record the HTTP server in node.js and integrate it with the React app

Single-page applications (SPA) written with React have a problem: the page does not appear until the Javascript code is received and executed by the client. This means that clients that do not run Javascript cannot see anything - for example, Google crawler. Therefore, if you want your page to be indexed, you need to figure out a way to serve the fully displayed page when the client makes a request. The solution to this problem is server side rendering. This is a pretty complicated topic, and I would advise you to do some kind of Google if you are interested in it.

Now about your question: which is better? As always, it depends on your needs.

One of my projects is an outdated PHP application in which I rewrite part of the interface code to use React components. Do I need node.js HTTP server or server rendering? No, absolutely not. But I use Babel and Browserify to make my life easier as a developer.

My other personal project is a small SPA, written using the Next.js framework, which is quite advanced, including server-side rendering. Of course, I could write this project using other technologies, but I like the general code base between the client and the server, which gives it.

+6
source share

All Articles