1. Node application architecture design
2. The React isomorphism
4. CDN and code discovery on front-end code
6. Grayscale environment
On the Node
Node is a JavaScript runtime based on the Chrome V8 engine. Created in 2009, Node was the first to bring JavaScript to back-end server development, as well as authoring tools such as code packaging tools, but it was originally intended to be a high-performance Web server. Its internal implementation of asynchronous IO, event-driven is designed for high-performance Web services.
Demand background
At the end of 2019, netease Smart Enterprise is building an SCRM product — netease Huke (https://huke.163.com), which initially has three requirements:
1. Mutual customer platform.
3, mutual visitors official website.
The first two sections are more interactive, with some requirements that technically favor the single-page application format. The official website also needs SEO, so it needs isomorphic rendering capability (use React framework at the front end); Also given the technical architecture to promote the efficiency of development has formed the bottleneck, so consider using the new technology, to complete before and after the liberation of productive forces and eventually consider before and after using the Node to achieve the complete separation, thoroughly solved before the front-end to write Java template file and the end of the page data before and after understanding inconsistent embarrassing situation.
1. Page rendering.
3. Fill in the initial necessary data of the page.
Another goal is to gradually improve the Node engineering tool system of smart enterprises through this project, and eventually form their own Node ecology.
Design and implementation
Briefly describe the access path for the next complete user request. First, the user requests to the gateway, which forwards the request to Node or Java application according to the URL forwarding rules, thus completing a page visit or interface request. This involves routing design, and the URL of the page and interface should be able to be distinguished by path.
Another important problem is the user’s login information. We use a more traditional scheme. The user login function is implemented in the Java side. Redirect the user to the login page. When the user fills in the information and clicks the login button, the login interface on the Java side is called for login. After the login request succeeds, the Java side will attach cookies to the response, so that the login information on the front-end, Node and Java side can be connected.
About the isomorphism
A set of code that can be run on both the server side and the client side, executed once on the server side to implement server-side rendering and again on the client side to take over page interaction is an isomorphic application. In short, it is a combination of server-side direct output and client-side rendering that fully combines the advantages of both and effectively avoids the disadvantages of both.
How do React render objects in Node? This is because React introduced the virtual DOM. The virtual DOM is a JavaScript object mapping of the real DOM. When React does page operations, it doesn’t actually manipulate the DOM directly. That’s what made SSR possible. React outputs the virtual DOM as a string on the Node side, and maps the virtual DOM to the real DOM on the browser side to complete the page rendering.
We chose the ‘renderToString’ method based on the requirements.
Let’s introduce the differences of our implementation, first of all, configuration:
Json fishssr: {routes: [{path: '/admin/*', Component: () => (require(' @/page/admin ').default), controller: 'page. Admin'}, {path: '/ user / *, Component: () = > (the require (' @ / page/user). The default), the controller: 'user.h5Page',},], // page template file path template: 'screen/index.html', // server render packed JS file serverJs: ResolvePath (' dist/Page. Server. Js'),} ` ` `Copy the code
Path: ‘/admin/*’ and ‘/user/*’ correspond to a single page application respectively.
Component: The React Component of the page processes the initial data internally and converts it to the store’s preloadedState or props, which uses front-end routing.
Template: a template file for the React page. The internal ‘stream’ is the string generated after Node renders the React page component.
```html <! DOCTYPE HTML > < HTML lang= 'zh-CN' > <head> <title> <link rel= 'stylesheet' href= '/ CSS/page.css' /> </head> The < body > < div id = "app" > {{stream | safe}} < / div > < script > window. __INITIAL_DATA__ = {{initialData | safe}}; < / script > < script SRC = "/ js/runtime - Page. The js" > < / script > < script SRC = "/ js/Page. Js" > < / script > < / body > < / HTML > ` ` `Copy the code
```
const clientRender = async () => {
ReactDOM.hydrate(
<>
{
Routes.map(route => {
const { path, Component } = route
const isMatch = matchPath(window.location.pathname, route)
if ( !isMatch ) {
return null
}
const ActiveComponent = Component()
const WrappedComponent = GetInitialProps(ActiveComponent)
return <WrappedComponent key={path} />
})
}
</>, document.getElementById('app'))
}
const serverRender = async (params) => {
const { initData, path, url } = params
const ActiveComponent = getComponent(Routes, path)()
return (
<StaticRouter location={url} context={initData}>
<ActiveComponent {... initData} />
</StaticRouter>
)
}
export default __isBrowser__ ? clientRender() : serverRender
```
Copy the code
conclusion
At present, the product ** netease Huke ** using this solution has been launched. This solution has solved the technical and business requirements mentioned at the beginning of the article. At the same time, the new front and back end cooperation mode brought by this solution has greatly improved the development efficiency of not only the front end, but also very friendly to the back end. At the same time, the front end can also broaden its boundaries and accept more demands. For example, our operating system and functional API, such as wechat JS-SDK authentication, can only be put in the back end before, but now it is put in the Node end. The front end development is more flexible and reduces a lot of communication costs. However, as an external service Node application, only these are not enough, and still need the support of many engineering tools.
Listen to netease CTO talk about cutting-edge observation, see the most valuable technology dry goods, learn the latest practical experience of netease. Netease Smart Enterprise technology + will accompany you to grow from a thinker to a technical expert.