确实需要nodeJS在前端ENV吗? [英] does react really need nodeJS on the frontend ENV?

查看:98
本文介绍了确实需要nodeJS在前端ENV吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新人的反应。我想开始一个我自己的小世界示例。

I am new in react. I want to start a small hello world 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

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>

问题是 示例需要nodeJS(用于requeir()部分)和npm安装和npm开始......所有这些。

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

我可以在没有nodeJS的情况下差别这样做

I can do it diffrently 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

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>

在这个例子中我使用cdn来导入nodejs应该导入的react的依赖项npm安装阶段。问题是 - 哪个更好?我可以使用cdn而不是entriely使用nodejs吗?让nodejs和npm模块(或bower ..)有反应的东西更正确吗?

in this example I am using cdn in order to import the dependencies of react that nodejs should import in the npm install phase. question is - which is better? could I use just cdn and not use nodejs entriely? is it more correct to have nodejs and npm modules (or bower..) to have the react stuff?

谢谢

推荐答案

标题中提出的问题的答案是不,您不需要node.js在客户端使用React

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

事实上,你给出的第二个例子就是这样做 - 在客户端使用React而不提及node.js。

In fact, the second example you give is doing just that - using React on the client side without any mention of node.js.

也就是说,有几种不同的方法可以使用node.js,这些方法在构建基于React的应用程序时非常有用。

That said, there are a couple of different ways to use node.js that are very useful when building React-based applications.

使用基于node.js的构建工具,如 browserify webpack 将您的客户端代码捆绑到一个整洁,整洁的包中,然后将其提供给客户。

Use a node.js-based build tool like browserify or webpack to bundle your client-side code into a neat, tidy package which you then serve to the client.

例如,在我正在处理的项目中,我使用browserify构建一个保存在的单个Javascript文件public / js / bundle.js 包含在我的 index.html中的普通旧< script> 标记中并且它全部由Apache提供。这个包包含我的应用程序代码(包括一些React组件)以及我的应用程序代码的所有依赖项(包括 react react-dom ,除其他外)。客户端的主要好处是减少页面请求的数量和所需的带宽(因为我可以使用 UglifyJS 缩小整个捆绑包)。开发人员的主要好处是,您可以使用 Babel 等工具编写 .jsx 代码而不是普通的旧Javascript - 这极大地提高了工作效率。

For example, in a project I'm working on, I use browserify to build a single Javascript file saved at public/js/bundle.js which is included via a plain old <script> tag in my index.html and it's all served by Apache. This bundle contains my application code (including some React components) along with all the dependencies for my application code (including react and react-dom, among other things). The main benefit for the client is to reduce the number of requests for the page and the bandwidth required (since I can use UglifyJS to minify the whole bundle). The main benefit for the developer is that you can use tools such as Babel to write .jsx code instead of plain old Javascript - this is a tremendous boost to productivity.

在node.js中编写HTTP服务器

近年来,有很多关于使用node.js构建整个应用程序的嗡嗡声 - 客户端服务器端。这里的主要好处是代码重用:想象一下,你写了一个用日期做一些奇特的东西的库。如果你用Javascript编写客户端代码,用PHP编写服务器端代码,那么你必须重写那个库;如果您在两侧都使用node.js,则只需执行一次。

There has been a lot of buzz in recent years about building the entire app with node.js - client-side and server-side. The main benefit here is code reuse: imagine that you wrote a library that does something fancy with dates. If you're writing your client-side code in Javascript and your server-side code in PHP, then you'll have to rewrite that library; if you're using node.js on both sides, you only need to do it once.

在node.js中编写HTTP服务器并将其与您的React应用程序

使用React编写的单页面应用程序(SPA)存在问题:在收到Javascript代码之前,页面不会呈现由客户执行。这意味着不执行Javascript的客户端将看不到任何内容 - 例如Google的网络抓取工具。因此,如果您希望将页面编入索引,则在客户端发出请求时,您需要找出一些方法来提供完全呈现的页面。解决方案是服务器端的React渲染。这是一个非常具有挑战性的话题,如果你对此感兴趣,我鼓励你做一些谷歌搜索。

Single-page applications (SPAs) written with React have a problem: the page doesn't get rendered until the Javascript code is received and executed by the client. This means that clients that don't execute Javascript won't see anything - such as Google's web crawler. So if you want your page indexed, you'll need to figure out some way to serve a fully-rendered page when a client makes a request. The solution to this is server-side React rendering. This is quite a challenging topic, and I'd encourage you to do some Googling if your interested in it.

现在你的问题:哪个更好一如既往,这取决于您的需求。

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

我的一个项目是遗留的PHP应用程序,我正在重写一些前端代码使用React组件。我是否需要node.js HTTP服务器或服务器端呈现?一点都不。但我正在使用Babel和Browserify让我的开发人员生活更轻松。

One of my projects is a legacy PHP application where I'm rewriting some of the front-end code to use React components. Do I need a node.js HTTP server or server-side rendering? No, not at all. But I am using Babel and Browserify to make my life as a developer easier.

我的另一个个人项目是使用名为 Next.js ,它非常先进,融合了服务器端渲染。我当然可以使用其他技术编写这个项目,但我确实喜欢它提供的客户端和服务器之间的共享代码库。

Another of my personal projects is a small SPA written using a framework called Next.js which is pretty cutting-edge, incorporating server-side rendering. I could certainly have written this project using other technologies, but I do like the shared codebase between client and server that this affords.

这篇关于确实需要nodeJS在前端ENV吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆