当我们已准备好后端时,为什么我们需要Express服务器

时间:2022-01-14 22:26:56

I am quite new to javascript and web application environment. I have seen a react web application project which had a public directory, a client directory and a server directory. I have few question

我是javascript和web应用程序环境的新手。我见过一个反应式Web应用程序项目,它有一个公共目录,一个客户端目录和一个服务器目录。我有几个问题

  1. Why do we need an express server file setup in the frontend project if we already have backend APIs ready and backend server ready

    如果我们已经准备好后端API并准备好后端服务器,为什么我们需要在前端项目中设置快速服务器文件?

  2. Do we need an express server if we make the frontend on react and call the APIs to fetch the data for application.

    如果我们让前端响应并调用API来获取应用程序的数据,我们是否需要一个快速服务器?

  3. Isn't the backend server and express server in the frontend project are same?

    前端服务器和前端项目中的快速服务器是不是一样的?

2 个解决方案

#1


5  

Why do we need an express server file setup in the frontend project if we already have backend APIs ready and backend server ready

如果我们已经准备好后端API并准备好后端服务器,为什么我们需要在前端项目中设置快速服务器文件?

You don't.

You need an HTTP server to listen for and respond to any Ajax requests you make from your client side code.

您需要一个HTTP服务器来监听并响应您从客户端代码发出的任何Ajax请求。

You need an HTTP server to listen for and respond to any requests for the HTML documents and static resources (JS, CSS, images, etc) that your pages need.

您需要一个HTTP服务器来监听和响应您的页面所需的HTML文档和静态资源(JS,CSS,图像等)的任何请求。

These can be the same HTTP server, different HTTP servers, written with Express or not written with Express.

这些可以是相同的HTTP服务器,不同的HTTP服务器,使用Express编写或不使用Express编写。

React tutorials tend to ignore mentioning this and just dive in showing how to use Express for everything. Don't read too much into that.

反应教程倾向于忽略提及这一点,并深入展示如何使用Express来实现一切。不要读太多。

Do we need an express server if we make the frontend on react and call the APIs to fetch the data for application.

如果我们让前端响应并调用API来获取应用程序的数据,我们是否需要一个快速服务器?

No. See above.

不,见上文。

Isn't the backend server and express server in the frontend project are same?

前端服务器和前端项目中的快速服务器是不是一样的?

Maybe. It is up to you. See above.

也许。它是由你决定。往上看。

#2


3  

There is no such thing as a "backend server" and a "frontend server", a simple web application is composed of two main parts:

没有“后端服务器”和“前端服务器”之类的东西,简单的Web应用程序由两个主要部分组成:

1/ an application that serves html pages, which runs on a backend, so it is usually called a server, but a typical cloud server nowadays can run hundreds of different serving apps at the same time

1 /一个提供html页面的应用程序,它运行在后端,所以它通常被称为服务器,但现在典型的云服务器可以同时运行数百个不同的服务应用程序

2/ a frontend, which is typically a complex piece of JavaScript software and html pages that are dynamically send to the user browser and execute locally

2 /一个前端,通常是一个复杂的JavaScript软件和html页面,动态发送到用户浏览器并在本地执行

The minimum that you require to have a working website is a server application that will return one or several html pages upon user request. A typical React + Node project is organized as follow:

拥有工作网站所需的最低要求是服务器应用程序,它将根据用户请求返回一个或多个html页面。典型的React + Node项目组织如下:

  • A server directory: which contains all the code for the serving app - the one returning the webpages, it can also contains code that handle the REST API, in case your client app requires dynamic data or if your server connect to a database. Note that the webpage server and the API server could be two different- or more - applications.

    服务器目录:包含服务应用程序的所有代码 - 返回网页的代码,它还可以包含处理REST API的代码,以防客户端应用程序需要动态数据或服务器连接到数据库。请注意,网页服务器和API服务器可以是两个不同或更多的应用程序。

  • You usually dont want to share to users your server code, so typically you have a public directory that contains the html pages and this is the only location on the disk - theoretically - that can be access by users. This directory can also contains required images and resources needed by the webpages, it is also called static resources

    您通常不希望向用户分享您的服务器代码,因此通常您有一个包含html页面的公共目录,这是磁盘上唯一的位置 - 理论上 - 可以由用户访问。此目录还可以包含网页所需的所需图像和资源,它也称为静态资源

  • To keep thing more organized, the code of the frontend application is placed in a client directory but on production is usually bundled in one or few files, depending on the size of the app, and also placed in the public directory, so it contains everything needed to serve the app.

    为了使事情更有条理,前端应用程序的代码放在客户端目录中,但生产通常捆绑在一个或几个文件中,具体取决于应用程序的大小,也放在公共目录中,因此它包含所有内容需要提供应用程序。

Hope it helps

希望能帮助到你

#1


5  

Why do we need an express server file setup in the frontend project if we already have backend APIs ready and backend server ready

如果我们已经准备好后端API并准备好后端服务器,为什么我们需要在前端项目中设置快速服务器文件?

You don't.

You need an HTTP server to listen for and respond to any Ajax requests you make from your client side code.

您需要一个HTTP服务器来监听并响应您从客户端代码发出的任何Ajax请求。

You need an HTTP server to listen for and respond to any requests for the HTML documents and static resources (JS, CSS, images, etc) that your pages need.

您需要一个HTTP服务器来监听和响应您的页面所需的HTML文档和静态资源(JS,CSS,图像等)的任何请求。

These can be the same HTTP server, different HTTP servers, written with Express or not written with Express.

这些可以是相同的HTTP服务器,不同的HTTP服务器,使用Express编写或不使用Express编写。

React tutorials tend to ignore mentioning this and just dive in showing how to use Express for everything. Don't read too much into that.

反应教程倾向于忽略提及这一点,并深入展示如何使用Express来实现一切。不要读太多。

Do we need an express server if we make the frontend on react and call the APIs to fetch the data for application.

如果我们让前端响应并调用API来获取应用程序的数据,我们是否需要一个快速服务器?

No. See above.

不,见上文。

Isn't the backend server and express server in the frontend project are same?

前端服务器和前端项目中的快速服务器是不是一样的?

Maybe. It is up to you. See above.

也许。它是由你决定。往上看。

#2


3  

There is no such thing as a "backend server" and a "frontend server", a simple web application is composed of two main parts:

没有“后端服务器”和“前端服务器”之类的东西,简单的Web应用程序由两个主要部分组成:

1/ an application that serves html pages, which runs on a backend, so it is usually called a server, but a typical cloud server nowadays can run hundreds of different serving apps at the same time

1 /一个提供html页面的应用程序,它运行在后端,所以它通常被称为服务器,但现在典型的云服务器可以同时运行数百个不同的服务应用程序

2/ a frontend, which is typically a complex piece of JavaScript software and html pages that are dynamically send to the user browser and execute locally

2 /一个前端,通常是一个复杂的JavaScript软件和html页面,动态发送到用户浏览器并在本地执行

The minimum that you require to have a working website is a server application that will return one or several html pages upon user request. A typical React + Node project is organized as follow:

拥有工作网站所需的最低要求是服务器应用程序,它将根据用户请求返回一个或多个html页面。典型的React + Node项目组织如下:

  • A server directory: which contains all the code for the serving app - the one returning the webpages, it can also contains code that handle the REST API, in case your client app requires dynamic data or if your server connect to a database. Note that the webpage server and the API server could be two different- or more - applications.

    服务器目录:包含服务应用程序的所有代码 - 返回网页的代码,它还可以包含处理REST API的代码,以防客户端应用程序需要动态数据或服务器连接到数据库。请注意,网页服务器和API服务器可以是两个不同或更多的应用程序。

  • You usually dont want to share to users your server code, so typically you have a public directory that contains the html pages and this is the only location on the disk - theoretically - that can be access by users. This directory can also contains required images and resources needed by the webpages, it is also called static resources

    您通常不希望向用户分享您的服务器代码,因此通常您有一个包含html页面的公共目录,这是磁盘上唯一的位置 - 理论上 - 可以由用户访问。此目录还可以包含网页所需的所需图像和资源,它也称为静态资源

  • To keep thing more organized, the code of the frontend application is placed in a client directory but on production is usually bundled in one or few files, depending on the size of the app, and also placed in the public directory, so it contains everything needed to serve the app.

    为了使事情更有条理,前端应用程序的代码放在客户端目录中,但生产通常捆绑在一个或几个文件中,具体取决于应用程序的大小,也放在公共目录中,因此它包含所有内容需要提供应用程序。

Hope it helps

希望能帮助到你