


Knowledge about front-end and back-end separation and practical nodejs middle-tier services
1. Background
The book continues from the above, briefly discussing the separation and practice of front-end and back-end (1) We use mock server to build our own front-end data simulation service. During the front-end and back-end development process, we only need to define Interface specifications allow each other to carry out their respective development tasks. During joint debugging, just perform data joint debugging according to the previously defined development specifications. The functions of the front and back ends are clearer:
Back end | Front end |
---|---|
Provide data | Receive data, return data |
Process business logic | Process rendering logic |
Server-side MVC architecture | Client-side MV* architecture |
Code runs on the server | Code runs on the browser |
The separation here is clean and the division of labor is very clear. It seems that everything is so beautiful, but... we can also easily find the problem:
Client-side Model It is the processing of Server-side Model
Client-side View and Server-side are things at different levels
Client-side Controller and The Controller on the Sever-side has its own
Route on the Client-side, but the Server-side may not have
That is to say, the server-side and The responsibilities of each layer of the client overlap, and everyone does their own thing, making it difficult to unify the specific things to be done. And it may be accompanied by some performance problems. The most specific manifestation is our commonly used SPA application:
Rendering, the value is all done on the client, there are performance issues
Required Waiting for resources to be available before proceeding, there will be a brief white screen and flashing
The experience on a mobile device with a low-speed network is extremely different
Rendering is all on the client side, templates cannot be reused, and SEO implementation is troublesome
Then, the amount of our code becomes larger and larger, and we need to verify more and more forms. Sometimes, the front-end submission needs to verify the form once.
The server needs to be verified to achieve data reliability; the front-end routing may not exist on the server....etc. This series of reusability issues. So our previous reconstruction may require deeper thinking.
2. Start refactoring
Before we start refactoring, we need to divide the front-end and back-end boundaries, that is to say, what belongs to the front-end category and what belongs to the back-end category. The most traditional division of front-end and back-end may be like this:
Then the question arises: the wiring of our front-end and back-end division is divided according to job responsibilities. front-end and back-end; or front-end and back-end divided according to the hardware environment? Since nodejs, we can redefine the scope of front-end and back-end in terms of job functions:
As you can see, there are more front-ends here than before. nodejs, that is, we built a nodejs service as the middle layer between the front and back ends!
Why do we choose nodejs as the middle layer? Because we put the middle layer in the front-end category, for front-end friends, nodejs is still js after all, so from a grammatical point of view, there should be no problem in folding it up. Secondly, the cost of development transfer is also low, and there is no need to switch back and forth between language logic and syntax:
The front-end is familiar with the language, and the learning cost is low
Both are JS, can be reused on the front and back ends
Suitable for: event-driven, non-blocking I/O
Suitable for IO-intensive business
The execution speed is not bad either
Okay, having said so many things in advance, what can this middle layer bring to us? ? You must know that the development cost of introducing nodejs is also very high. First of all, it is an extra layer of services. Not to mention many more, just in terms of transmission time, there is an extra layer of transmission time! Next, let's study what application scenarios nodejs can bring us more advantages than disadvantages.
3. Start the middle layer journey
After introducing nodejs, let’s re-divide the functions of the front-end and back-end:
This is the main idea of the middle layer nodejs. Let’s take a look at common business scenarios:
1. Interface data reliability repair
Sometimes the server returns to us The data may not be the structure the front-end wants. All display data used is provided by the back-end through an asynchronous interface (AJAX/JSONP), and the front-end only displays it. However, the backend often provides backend data logic, and the frontend also needs to process this data logic. For example, when I develop a function, I sometimes encounter this problem:
A certain field returned by the server is null or the data structure returned by the server is too deep. The front end needs to continuously write such code to determine whether the data structure really returns the correct thing, rather than null or undefined:
if (params.items && params.items.type && ...) { // todo }
In this case, our front-end should not repeatedly verify the format of the data, and this should not be what browser-side js needs to do. We can do interface forwarding in the middle layer and perform data processing during the forwarding process. No need to worry about data return:
router.get('/buyer/product/detail', (req, res, next) => { httpRequest.get('/buyer/product/detail', (data) => { // todo 处理数据 res.send(data); }) })
2. 页面性能优化 和 SEO
有点时候我们做单页面应用,经常会碰到首屏加载性能问题,这个时候如果我们接了中间层nodejs的话,那么我们可以把首屏渲染的任务交给nodejs去做,次屏的渲染依然走之前的浏览器渲染。(前端换页,浏览器端渲染,直接输入网址,服务器渲染)服务端渲染对页面进行拼接直出html字符串,可以大幅提高首屏渲染的时间,减少用户的等待时间。这种形式应用最广的比如 Vue 的服务端渲染,里面也有相关的介绍。
其次对于单页面的SEO优化也是很好地处理方式,由于目前的ajax并不被搜索百度等搜索引擎支持,所以如果想要得到爬虫的支持,那么服务端渲染也是一种解决方法。(PS:如果觉得服务端渲染太麻烦,我这里还有一篇介绍处理SEO的另一种思路处理 Vue 单页面 Meta SEO的另一种思路可以参考)
3. 淘宝常见的需求解决方案
需求:在淘宝,单日四亿PV,页面数据来自各个不同接口,为了不影响体验,先产生页面框架后,在发起多个异步请求取数据更新页面,这些多出来的请求带来的影响不小,尤其在无线端。
解决方案:在NodeJS端使用 Bigpiper 技术,合并请求,降低负担,分批输出,不影响体验。同时可以拆分大接口为独立小接口,并发请求。串行 => 并行,大幅缩短请求时间。
The above is the detailed content of Knowledge about front-end and back-end separation and practical nodejs middle-tier services. For more information, please follow other related articles on the PHP Chinese website!

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Mac version
God-level code editing software (SublimeText3)
