{"componentChunkName":"component---src-templates-content-template-js","path":"/en/part7/miscellaneous","result":{"data":{"markdownRemark":{"html":"<div class=\"content\">\n<h3>Class Components</h3>\n<p>During the course, we have only used React components having been defined as JavaScript functions. This was not possible without the <a href=\"https://reactjs.org/docs/hooks-intro.html\">hook</a> functionality that came with version 16.8 of React. Before, when defining a component that uses state, one had to define it using JavaScript's <a href=\"https://reactjs.org/docs/state-and-lifecycle.html#converting-a-function-to-a-class\">Class</a> syntax.</p>\n<p>It is beneficial to at least be familiar with Class Components to some extent since the world contains a lot of old React code, which will probably never be completely rewritten using the updated syntax.</p>\n<p>Let's get to know the main features of Class Components by producing yet another very familiar anecdote application. We store the anecdotes in the file <i>db.json</i> using <i>json-server</i>. The contents of the file are taken from <a href=\"https://github.com/fullstack-hy/misc/blob/master/anecdotes.json\">here</a>.</p>\n<p>The initial version of the Class Component looks like this</p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">import</span> React <span class=\"token keyword\">from</span> <span class=\"token string\">'react'</span>\n\n<span class=\"token keyword\">class</span> <span class=\"token class-name\">App</span> <span class=\"token keyword\">extends</span> <span class=\"token class-name\">React<span class=\"token punctuation\">.</span>Component</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token function\">constructor</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">props</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">super</span><span class=\"token punctuation\">(</span>props<span class=\"token punctuation\">)</span>\n  <span class=\"token punctuation\">}</span>\n\n  <span class=\"token function\">render</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">return</span> <span class=\"token punctuation\">(</span>\n      <span class=\"token operator\">&lt;</span>div<span class=\"token operator\">></span>\n        <span class=\"token operator\">&lt;</span>h1<span class=\"token operator\">></span>anecdote <span class=\"token keyword\">of</span> the day<span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>h1<span class=\"token operator\">></span>\n      <span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n    <span class=\"token punctuation\">)</span>\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">default</span> App</code></pre></div>\n<p>The component now has a <a href=\"https://react.dev/reference/react/Component#constructor\">constructor</a>, in which nothing happens at the moment, and contains the method <a href=\"https://react.dev/reference/react/Component#render\">render</a>. As one might guess, render defines how and what is rendered to the screen.</p>\n<p>Let's define a state for the list of anecdotes and the currently-visible anecdote. In contrast to when using the <a href=\"https://react.dev/reference/react/useState\">useState</a> hook, Class Components only contain one state. So if the state is made up of multiple \"parts\", they should be stored as properties of the state. The state is initialized in the constructor:</p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">class</span> <span class=\"token class-name\">App</span> <span class=\"token keyword\">extends</span> <span class=\"token class-name\">React<span class=\"token punctuation\">.</span>Component</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token function\">constructor</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">props</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">super</span><span class=\"token punctuation\">(</span>props<span class=\"token punctuation\">)</span>\n\n<span class=\"gatsby-highlight-code-line\">    <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>state <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span></span><span class=\"gatsby-highlight-code-line\">      <span class=\"token literal-property property\">anecdotes</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span></span><span class=\"gatsby-highlight-code-line\">      <span class=\"token literal-property property\">current</span><span class=\"token operator\">:</span> <span class=\"token number\">0</span></span><span class=\"gatsby-highlight-code-line\">    <span class=\"token punctuation\">}</span></span>  <span class=\"token punctuation\">}</span>\n\n  <span class=\"token function\">render</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n<span class=\"gatsby-highlight-code-line\">    <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>state<span class=\"token punctuation\">.</span>anecdotes<span class=\"token punctuation\">.</span>length <span class=\"token operator\">===</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></span><span class=\"gatsby-highlight-code-line\">      <span class=\"token keyword\">return</span> <span class=\"token operator\">&lt;</span>div<span class=\"token operator\">></span>no anecdotes<span class=\"token operator\">...</span><span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span></span><span class=\"gatsby-highlight-code-line\">    <span class=\"token punctuation\">}</span></span>\n    <span class=\"token keyword\">return</span> <span class=\"token punctuation\">(</span>\n      <span class=\"token operator\">&lt;</span>div<span class=\"token operator\">></span>\n        <span class=\"token operator\">&lt;</span>h1<span class=\"token operator\">></span>anecdote <span class=\"token keyword\">of</span> the day<span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>h1<span class=\"token operator\">></span>\n<span class=\"gatsby-highlight-code-line\">        <span class=\"token operator\">&lt;</span>div<span class=\"token operator\">></span></span><span class=\"gatsby-highlight-code-line\">          <span class=\"token punctuation\">{</span><span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>state<span class=\"token punctuation\">.</span>anecdotes<span class=\"token punctuation\">[</span><span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>state<span class=\"token punctuation\">.</span>current<span class=\"token punctuation\">]</span><span class=\"token punctuation\">.</span>content<span class=\"token punctuation\">}</span></span><span class=\"gatsby-highlight-code-line\">        <span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span></span><span class=\"gatsby-highlight-code-line\">        <span class=\"token operator\">&lt;</span>button<span class=\"token operator\">></span>next<span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>button<span class=\"token operator\">></span></span>      <span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n    <span class=\"token punctuation\">)</span>\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>The component state is in the instance variable <i>this.state</i>. The state is an object having two properties. <i>this.state.anecdotes</i> is the list of anecdotes and <i>this.state.current</i> is the index of the currently-shown anecdote.</p>\n<p>In Functional components, the right place for fetching data from a server is inside an <a href=\"https://react.dev/reference/react/useEffect\">effect hook</a>, which is executed when a component renders or less frequently if necessary, e.g. only in combination with the first render.</p>\n<p>The <a href=\"https://react.dev/reference/react/Component#adding-lifecycle-methods-to-a-class-component\">lifecycle methods</a> of Class Components offer corresponding functionality. The correct place to trigger the fetching of data from a server is inside the lifecycle method <a href=\"https://react.dev/reference/react/Component#componentdidmount\">componentDidMount</a>, which is executed once right after the first time a component renders:</p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">class</span> <span class=\"token class-name\">App</span> <span class=\"token keyword\">extends</span> <span class=\"token class-name\">React<span class=\"token punctuation\">.</span>Component</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token function\">constructor</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">props</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">super</span><span class=\"token punctuation\">(</span>props<span class=\"token punctuation\">)</span>\n\n    <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>state <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span>\n      <span class=\"token literal-property property\">anecdotes</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span>\n      <span class=\"token literal-property property\">current</span><span class=\"token operator\">:</span> <span class=\"token number\">0</span>\n    <span class=\"token punctuation\">}</span>\n  <span class=\"token punctuation\">}</span>\n\n<span class=\"gatsby-highlight-code-line\">  <span class=\"token function-variable function\">componentDidMount</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span></span><span class=\"gatsby-highlight-code-line\">    axios<span class=\"token punctuation\">.</span><span class=\"token function\">get</span><span class=\"token punctuation\">(</span><span class=\"token string\">'http://localhost:3001/anecdotes'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">then</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">response</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span></span><span class=\"gatsby-highlight-code-line\">      <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token function\">setState</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">anecdotes</span><span class=\"token operator\">:</span> response<span class=\"token punctuation\">.</span>data <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span></span><span class=\"gatsby-highlight-code-line\">    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span></span><span class=\"gatsby-highlight-code-line\">  <span class=\"token punctuation\">}</span></span>\n  <span class=\"token comment\">// ...</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>The callback function of the HTTP request updates the component state using the method <a href=\"https://react.dev/reference/react/Component#setstate\">setState</a>. The method only touches the keys that have been defined in the object passed to the method as an argument. The value for the key <i>current</i> remains unchanged.</p>\n<p>Calling the method setState always triggers the rerender of the Class Component, i.e. calling the method <i>render</i>.</p>\n<p>We'll finish off the component with the ability to change the shown anecdote. The following is the code for the entire component with the addition highlighted:</p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">class</span> <span class=\"token class-name\">App</span> <span class=\"token keyword\">extends</span> <span class=\"token class-name\">React<span class=\"token punctuation\">.</span>Component</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token function\">constructor</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">props</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">super</span><span class=\"token punctuation\">(</span>props<span class=\"token punctuation\">)</span>\n\n    <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>state <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span>\n      <span class=\"token literal-property property\">anecdotes</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span>\n      <span class=\"token literal-property property\">current</span><span class=\"token operator\">:</span> <span class=\"token number\">0</span>\n    <span class=\"token punctuation\">}</span>\n  <span class=\"token punctuation\">}</span>\n\n  <span class=\"token function-variable function\">componentDidMount</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n    axios<span class=\"token punctuation\">.</span><span class=\"token function\">get</span><span class=\"token punctuation\">(</span><span class=\"token string\">'http://localhost:3001/anecdotes'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">then</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">response</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n      <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token function\">setState</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">anecdotes</span><span class=\"token operator\">:</span> response<span class=\"token punctuation\">.</span>data <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n  <span class=\"token punctuation\">}</span>\n\n<span class=\"gatsby-highlight-code-line\">  <span class=\"token function-variable function\">handleClick</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span></span><span class=\"gatsby-highlight-code-line\">    <span class=\"token keyword\">const</span> current <span class=\"token operator\">=</span> Math<span class=\"token punctuation\">.</span><span class=\"token function\">floor</span><span class=\"token punctuation\">(</span></span><span class=\"gatsby-highlight-code-line\">      Math<span class=\"token punctuation\">.</span><span class=\"token function\">random</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">*</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>state<span class=\"token punctuation\">.</span>anecdotes<span class=\"token punctuation\">.</span>length</span><span class=\"gatsby-highlight-code-line\">    <span class=\"token punctuation\">)</span></span><span class=\"gatsby-highlight-code-line\">    <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token function\">setState</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> current <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span></span><span class=\"gatsby-highlight-code-line\">  <span class=\"token punctuation\">}</span></span>\n  <span class=\"token function\">render</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>state<span class=\"token punctuation\">.</span>anecdotes<span class=\"token punctuation\">.</span>length <span class=\"token operator\">===</span> <span class=\"token number\">0</span> <span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n      <span class=\"token keyword\">return</span> <span class=\"token operator\">&lt;</span>div<span class=\"token operator\">></span>no anecdotes<span class=\"token operator\">...</span><span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n    <span class=\"token punctuation\">}</span>\n\n    <span class=\"token keyword\">return</span> <span class=\"token punctuation\">(</span>\n      <span class=\"token operator\">&lt;</span>div<span class=\"token operator\">></span>\n        <span class=\"token operator\">&lt;</span>h1<span class=\"token operator\">></span>anecdote <span class=\"token keyword\">of</span> the day<span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>h1<span class=\"token operator\">></span>\n        <span class=\"token operator\">&lt;</span>div<span class=\"token operator\">></span><span class=\"token punctuation\">{</span><span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>state<span class=\"token punctuation\">.</span>anecdotes<span class=\"token punctuation\">[</span><span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>state<span class=\"token punctuation\">.</span>current<span class=\"token punctuation\">]</span><span class=\"token punctuation\">.</span>content<span class=\"token punctuation\">}</span><span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n<span class=\"gatsby-highlight-code-line\">        <span class=\"token operator\">&lt;</span>button onClick<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span><span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>handleClick<span class=\"token punctuation\">}</span><span class=\"token operator\">></span>next<span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>button<span class=\"token operator\">></span></span>      <span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n    <span class=\"token punctuation\">)</span>\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>For comparison, here is the same application as a Functional component:</p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">const</span> <span class=\"token function-variable function\">App</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">const</span> <span class=\"token punctuation\">[</span>anecdotes<span class=\"token punctuation\">,</span> setAnecdotes<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token function\">useState</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span>\n  <span class=\"token keyword\">const</span> <span class=\"token punctuation\">[</span>current<span class=\"token punctuation\">,</span> setCurrent<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token function\">useState</span><span class=\"token punctuation\">(</span><span class=\"token number\">0</span><span class=\"token punctuation\">)</span>\n\n  <span class=\"token function\">useEffect</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span><span class=\"token punctuation\">{</span>\n    axios<span class=\"token punctuation\">.</span><span class=\"token function\">get</span><span class=\"token punctuation\">(</span><span class=\"token string\">'http://localhost:3001/anecdotes'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">then</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">response</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n      <span class=\"token function\">setAnecdotes</span><span class=\"token punctuation\">(</span>response<span class=\"token punctuation\">.</span>data<span class=\"token punctuation\">)</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span>\n\n  <span class=\"token keyword\">const</span> <span class=\"token function-variable function\">handleClick</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n    <span class=\"token function\">setCurrent</span><span class=\"token punctuation\">(</span>Math<span class=\"token punctuation\">.</span><span class=\"token function\">round</span><span class=\"token punctuation\">(</span>Math<span class=\"token punctuation\">.</span><span class=\"token function\">random</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">*</span> <span class=\"token punctuation\">(</span>anecdotes<span class=\"token punctuation\">.</span>length <span class=\"token operator\">-</span> <span class=\"token number\">1</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n  <span class=\"token punctuation\">}</span>\n\n  <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>anecdotes<span class=\"token punctuation\">.</span>length <span class=\"token operator\">===</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">return</span> <span class=\"token operator\">&lt;</span>div<span class=\"token operator\">></span>no anecdotes<span class=\"token operator\">...</span><span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n  <span class=\"token punctuation\">}</span>\n\n  <span class=\"token keyword\">return</span> <span class=\"token punctuation\">(</span>\n    <span class=\"token operator\">&lt;</span>div<span class=\"token operator\">></span>\n      <span class=\"token operator\">&lt;</span>h1<span class=\"token operator\">></span>anecdote <span class=\"token keyword\">of</span> the day<span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>h1<span class=\"token operator\">></span>\n      <span class=\"token operator\">&lt;</span>div<span class=\"token operator\">></span><span class=\"token punctuation\">{</span>anecdotes<span class=\"token punctuation\">[</span>current<span class=\"token punctuation\">]</span><span class=\"token punctuation\">.</span>content<span class=\"token punctuation\">}</span><span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n      <span class=\"token operator\">&lt;</span>button onClick<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>handleClick<span class=\"token punctuation\">}</span><span class=\"token operator\">></span>next<span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>button<span class=\"token operator\">></span>\n    <span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n  <span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>In the case of our example, the differences were minor. The biggest difference between Functional components and Class components is mainly that the state of a Class component is a single object, and that the state is updated using the method <i>setState</i>, while in Functional components the state can consist of multiple different variables, with all of them having their own update function.</p>\n<p>In 2026, Class Components are largely a historical artifact. All modern React development uses Functional components with hooks, and there is no rational reason to reach for a Class component when writing new code. The React documentation itself treats Class components as a legacy API.</p>\n<h3>Error boundary</h3>\n<p>Even though Class Components are largely obsolete, there is one situation where you still cannot avoid them: <a href=\"https://react.dev/reference/react/Component#catching-rendering-errors-with-an-error-boundary\">error boundaries</a>. An error boundary is a component that catches JavaScript errors anywhere in its child component tree and displays a fallback UI instead of crashing the whole application. As of 2026, React has not yet introduced a hook-based alternative for this, so error boundaries must still be implemented as Class components.</p>\n<p>An error boundary looks like this:</p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">import</span> React <span class=\"token keyword\">from</span> <span class=\"token string\">'react'</span>\n\n<span class=\"token keyword\">class</span> <span class=\"token class-name\">ErrorBoundary</span> <span class=\"token keyword\">extends</span> <span class=\"token class-name\">React<span class=\"token punctuation\">.</span>Component</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token function\">constructor</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">props</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">super</span><span class=\"token punctuation\">(</span>props<span class=\"token punctuation\">)</span>\n    <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>state <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">hasError</span><span class=\"token operator\">:</span> <span class=\"token boolean\">false</span><span class=\"token punctuation\">,</span> <span class=\"token literal-property property\">error</span><span class=\"token operator\">:</span> <span class=\"token keyword\">null</span> <span class=\"token punctuation\">}</span>\n  <span class=\"token punctuation\">}</span>\n\n  <span class=\"token keyword\">static</span> <span class=\"token function\">getDerivedStateFromError</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">error</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">return</span> <span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">hasError</span><span class=\"token operator\">:</span> <span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span> error <span class=\"token punctuation\">}</span>\n  <span class=\"token punctuation\">}</span>\n\n  <span class=\"token function\">componentDidCatch</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">error<span class=\"token punctuation\">,</span> info</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    console<span class=\"token punctuation\">.</span><span class=\"token function\">error</span><span class=\"token punctuation\">(</span><span class=\"token string\">'ErrorBoundary caught an error'</span><span class=\"token punctuation\">,</span> error<span class=\"token punctuation\">,</span> info<span class=\"token punctuation\">)</span>\n  <span class=\"token punctuation\">}</span>\n\n  <span class=\"token function\">render</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>state<span class=\"token punctuation\">.</span>hasError<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n      <span class=\"token keyword\">return</span> <span class=\"token punctuation\">(</span>\n        <span class=\"token operator\">&lt;</span>div<span class=\"token operator\">></span>\n          <span class=\"token operator\">&lt;</span>h2<span class=\"token operator\">></span>Something went wrong<span class=\"token punctuation\">.</span><span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>h2<span class=\"token operator\">></span>\n          <span class=\"token operator\">&lt;</span>p<span class=\"token operator\">></span><span class=\"token punctuation\">{</span><span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>state<span class=\"token punctuation\">.</span>error<span class=\"token punctuation\">.</span>message<span class=\"token punctuation\">}</span><span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>p<span class=\"token operator\">></span>\n          <span class=\"token operator\">&lt;</span>button onClick<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token function\">setState</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">hasError</span><span class=\"token operator\">:</span> <span class=\"token boolean\">false</span><span class=\"token punctuation\">,</span> <span class=\"token literal-property property\">error</span><span class=\"token operator\">:</span> <span class=\"token keyword\">null</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">}</span><span class=\"token operator\">></span>\n            <span class=\"token keyword\">try</span> again\n          <span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>button<span class=\"token operator\">></span>\n        <span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n      <span class=\"token punctuation\">)</span>\n    <span class=\"token punctuation\">}</span>\n\n    <span class=\"token keyword\">return</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>props<span class=\"token punctuation\">.</span>children\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">default</span> ErrorBoundary</code></pre></div>\n<p>The two key lifecycle methods are <i>getDerivedStateFromError</i>, which updates state so the next render shows the fallback UI, and <i>componentDidCatch</i>, which is a good place to log the error to an error reporting service.</p>\n<p>You can wrap any part of your component tree with an error boundary to contain failures to that subtree:</p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">const</span> <span class=\"token function-variable function\">App</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">return</span> <span class=\"token punctuation\">(</span>\n    <span class=\"token operator\">&lt;</span>div<span class=\"token operator\">></span>\n      <span class=\"token operator\">&lt;</span>ErrorBoundary<span class=\"token operator\">></span>\n        <span class=\"token operator\">&lt;</span>Notes <span class=\"token operator\">/</span><span class=\"token operator\">></span>\n      <span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>ErrorBoundary<span class=\"token operator\">></span>\n      <span class=\"token operator\">&lt;</span>ErrorBoundary<span class=\"token operator\">></span>\n        <span class=\"token operator\">&lt;</span>Persons <span class=\"token operator\">/</span><span class=\"token operator\">></span>\n      <span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>ErrorBoundary<span class=\"token operator\">></span>\n    <span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n  <span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>If <i>Notes</i> throws an error, only that section shows the fallback. <i>Persons</i> continues to work normally.</p>\n<p>Because this is the one remaining use case for Class components, many projects use  the <a href=\"https://github.com/bvaughn/react-error-boundary\">react-error-boundary</a> library, which wraps the class-based machinery behind a convenient Functional component API so you never have to write a Class component yourself.</p>\n<h3>Frontend and backend in the same repository</h3>\n<p>During the course, we have created the frontend and backend into separate repositories. This is a very typical approach. However, we did the deployment by <a href=\"/en/part3/deploying_app_to_internet#serving-static-files-from-the-backend\">copying</a> the bundled frontend code into the backend repository. A possibly better approach would have been to deploy the frontend code separately.</p>\n<p>Sometimes the entire application is put into a single repository. A common and clean way to do this with a modern stack is to keep the Vite frontend in a <i>client</i> directory and the Express backend in a <i>server</i> directory, each with their own <i>package.json</i>. The root of the repository gets a third <i>package.json</i> that acts as a convenience wrapper with scripts to run both together.</p>\n<p>A minimal layout of such a <a href=\"https://github.com/fullstack-hy2020/monorepo\">repository</a> looks like this:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">app/\n  package.json        (root, scripts only)\n  client/\n    package.json      (Vite + React)\n    vite.config.js\n    src/\n      App.jsx\n  server/\n    package.json      (Express)\n    index.js</code></pre></div>\n<p>The Express server in <i>server/index.js</i> serves the API and, in production, also serves the built frontend from the <i>client/dist</i> directory:</p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">const</span> express <span class=\"token operator\">=</span> <span class=\"token function\">require</span><span class=\"token punctuation\">(</span><span class=\"token string\">'express'</span><span class=\"token punctuation\">)</span>\n<span class=\"token keyword\">const</span> path <span class=\"token operator\">=</span> <span class=\"token function\">require</span><span class=\"token punctuation\">(</span><span class=\"token string\">'path'</span><span class=\"token punctuation\">)</span>\n\n<span class=\"token keyword\">const</span> app <span class=\"token operator\">=</span> <span class=\"token function\">express</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n\napp<span class=\"token punctuation\">.</span><span class=\"token function\">use</span><span class=\"token punctuation\">(</span>express<span class=\"token punctuation\">.</span><span class=\"token function\">json</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n\napp<span class=\"token punctuation\">.</span><span class=\"token function\">get</span><span class=\"token punctuation\">(</span><span class=\"token string\">'/api/ping'</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">req<span class=\"token punctuation\">,</span> res</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n  res<span class=\"token punctuation\">.</span><span class=\"token function\">json</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">message</span><span class=\"token operator\">:</span> <span class=\"token string\">'pong'</span><span class=\"token punctuation\">,</span> <span class=\"token literal-property property\">time</span><span class=\"token operator\">:</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">Date</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">toISOString</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n\n<span class=\"token comment\">// serve the built Vite frontend in production</span>\n<span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>process<span class=\"token punctuation\">.</span>env<span class=\"token punctuation\">.</span><span class=\"token constant\">NODE_ENV</span> <span class=\"token operator\">===</span> <span class=\"token string\">'production'</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  app<span class=\"token punctuation\">.</span><span class=\"token function\">use</span><span class=\"token punctuation\">(</span>express<span class=\"token punctuation\">.</span><span class=\"token function\">static</span><span class=\"token punctuation\">(</span>path<span class=\"token punctuation\">.</span><span class=\"token function\">join</span><span class=\"token punctuation\">(</span>__dirname<span class=\"token punctuation\">,</span> <span class=\"token string\">'../client/dist'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n  app<span class=\"token punctuation\">.</span><span class=\"token function\">get</span><span class=\"token punctuation\">(</span><span class=\"token string\">'/*splat'</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">req<span class=\"token punctuation\">,</span> res</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n    res<span class=\"token punctuation\">.</span><span class=\"token function\">sendFile</span><span class=\"token punctuation\">(</span>path<span class=\"token punctuation\">.</span><span class=\"token function\">join</span><span class=\"token punctuation\">(</span>__dirname<span class=\"token punctuation\">,</span> <span class=\"token string\">'../client/dist/index.html'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">const</span> <span class=\"token constant\">PORT</span> <span class=\"token operator\">=</span> process<span class=\"token punctuation\">.</span>env<span class=\"token punctuation\">.</span><span class=\"token constant\">PORT</span> <span class=\"token operator\">||</span> <span class=\"token number\">3001</span>\napp<span class=\"token punctuation\">.</span><span class=\"token function\">listen</span><span class=\"token punctuation\">(</span><span class=\"token constant\">PORT</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">server running on port </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span><span class=\"token constant\">PORT</span><span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span></code></pre></div>\n<p>During development, the Vite dev server runs on its own port and needs to forward API requests to Express. This is configured in <i>client/vite.config.js</i>:</p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> defineConfig <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'vite'</span>\n<span class=\"token keyword\">import</span> react <span class=\"token keyword\">from</span> <span class=\"token string\">'@vitejs/plugin-react'</span>\n\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">default</span> <span class=\"token function\">defineConfig</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n  <span class=\"token literal-property property\">plugins</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span><span class=\"token function\">react</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span>\n  <span class=\"token literal-property property\">server</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token literal-property property\">proxy</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n      <span class=\"token string-property property\">'/api'</span><span class=\"token operator\">:</span> <span class=\"token string\">'http://localhost:3001'</span><span class=\"token punctuation\">,</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span></code></pre></div>\n<p>With the proxy in place, a frontend fetch to <i>/api/ping</i> is automatically forwarded to the Express server during development, so you never have to hard-code the backend URL.</p>\n<p>The root <i>package.json</i> ties everything together with a couple of scripts:</p>\n<div class=\"gatsby-highlight\" data-language=\"json\"><pre class=\"language-json\"><code class=\"language-json\"><span class=\"token punctuation\">{</span>\n  <span class=\"token property\">\"scripts\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token property\">\"dev\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"concurrently \\\"npm run dev --prefix server\\\" \\\"npm run dev --prefix client\\\"\"</span><span class=\"token punctuation\">,</span>\n    <span class=\"token property\">\"build\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"npm run build --prefix client\"</span><span class=\"token punctuation\">,</span>\n    <span class=\"token property\">\"start\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"NODE_ENV=production npm start --prefix server\"</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n  <span class=\"token property\">\"devDependencies\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token property\">\"concurrently\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"^8.0.0\"</span>\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>There are couple things interesting here.</p>\n<p>The <i>dev</i> script uses <a href=\"https://github.com/open-cli-tools/concurrently\">concurrently</a>, a small utility that runs multiple commands at the same time and merges their output into a single terminal stream. Without it you would have to open two separate terminals, one for the backend and one for the frontend. </p>\n<p>The <i>--prefix</i> flag tells npm which subdirectory to treat as the working directory for that command, so <i>npm run dev --prefix server</i> is equivalent to <i>cd server &#x26;&#x26; npm run dev</i>. </p>\n<p>Running <i>npm run dev</i> from the root therefore starts both the Vite dev server and Express in parallel with a single command. In this mode, Vite serves the frontend with hot module replacement: when you edit a React component, the browser updates instantly without a full page reload. The Express server runs separately and the Vite proxy forwards <i>/api</i> requests to it.</p>\n<p>Running <i>npm run build</i> compiles the frontend into the <i>client/dist</i> directory. After that, <i>npm start</i> sets <i>NODE_ENV=production</i> and starts Express, which picks up the static files from <i>client/dist</i> and serves both the API and the frontend from a single port. This is the setup you would use when deploying to a server.</p>\n<p>Because each part of the project has its own <i>package.json</i>, you need to be explicit about which one you are targeting when installing new packages. The same <i>--prefix</i> flag works for <i>npm install</i> as well:</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token function\">npm</span> <span class=\"token function\">install</span> axios <span class=\"token parameter variable\">--prefix</span> client     <span class=\"token comment\"># add to the frontend</span>\n<span class=\"token function\">npm</span> <span class=\"token function\">install</span> mongoose <span class=\"token parameter variable\">--prefix</span> server  <span class=\"token comment\"># add to the backend</span></code></pre></div>\n<p>Alternatively, you can simply <i>cd</i> into the directory and run <i>npm install</i> from there as you normally would.</p>\n<h3>Organization of code in React application</h3>\n<p>In most applications during this course, we followed the convention of placing components in a <i>components</i> directory, hooks in <i>hooks</i>, and server communication code in <i>services</i>. For the BlogList app that might look like this:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">src/\n  App.jsx\n  components/\n    Blog.jsx\n    BlogList.jsx\n    LoginForm.jsx\n    Notification.jsx\n  hooks/\n    useField.js\n  services/\n    blogs.js\n    users.js\n  stores/\n    blogStore.js\n    notificationStore.js</code></pre></div>\n<p>This flat, type-based grouping works well for small applications. </p>\n<p>When the app uses routing, it is common to add a <i>pages</i> directory (sometimes called <i>views</i>) for the top-level route components, keeping reusable UI components in <i>components</i>. This convention is used by frameworks such as <a href=\"https://nextjs.org/docs/pages/building-your-application/routing\">Next.js</a> and is described in the <a href=\"https://legacy.reactjs.org/docs/faq-structure.html\">React FAQ on file structure</a>:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">src/\n  App.jsx\n  pages/\n    HomePage.jsx\n    BlogPage.jsx\n    UserPage.jsx\n  components/\n    Blog.jsx\n    BlogList.jsx\n    LoginForm.jsx\n    Notification.jsx\n  hooks/\n    useField.js\n  services/\n    blogs.js\n    users.js\n  stores/\n    blogStore.js\n    notificationStore.js</code></pre></div>\n<p>As the codebase grows further, however, a change to a single feature may still touch files scattered across every directory, and both <i>components</i> and <i>pages</i> can become hard to navigate.</p>\n<p>A common response to this is to group files by <i>feature</i> instead. The <a href=\"https://feature-sliced.design/\">Feature-Sliced Design</a> methodology formalises this approach, and the <a href=\"https://github.com/alan2207/bulletproof-react\">bulletproof-react</a> project is a widely-referenced example of applying it in practice:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">src/\n  App.jsx\n  features/\n    blogs/\n      Blog.jsx\n      BlogList.jsx\n      blogService.js\n      blogStore.js\n    users/\n      UserList.jsx\n      userService.js\n    notifications/\n      Notification.jsx\n      notificationStore.js\n  hooks/\n    useField.js</code></pre></div>\n<p>Everything related to blogs lives together, so adding or changing a feature means working in one place rather than several. There is no single correct way to organize a larger project, and the right choice depends on the size and nature of the application.</p>\n<h3>Changes on the server</h3>\n<p>The applications we build during this course fetch data from the server when the page loads and after user actions, but they have no way of learning about changes made by other users. If a fellow user adds a new blog post, our frontend simply does not know about it until the page is refreshed. How can we keep the UI in sync with a server that changes independently?</p>\n<p>The simplest approach is <a href=\"https://en.wikipedia.org/wiki/Polling_(computer_science)\">polling</a>: the frontend repeatedly asks the server for fresh data at a fixed interval, for example using <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval\">setInterval</a>. Polling is easy to implement but wasteful, because most requests return nothing new.</p>\n<p>A cleaner alternative is <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API\">WebSockets</a>, which open a persistent two-way connection between the browser and the server. The server can then push updates to connected clients the moment something changes, without the client having to ask. WebSockets are now supported by all modern browsers.</p>\n<p>Working directly with the WebSocket API can be cumbersome. The <a href=\"https://socket.io/\">Socket.io</a> library wraps it with a higher-level API and adds automatic reconnection and other conveniences.</p>\n<p>In <a href=\"/en/part8\">part 8</a> we look at GraphQL, which includes a subscription mechanism that lets the server notify clients about data changes in a structured way.</p>\n<h3>React/node-application security</h3>\n<p>So far during the course, we have not touched on information security much. We do not have much time for this now either, but fortunately, University of Helsinki has a MOOC course <a href=\"https://cybersecuritybase.mooc.fi/module-2.1\">Securing Software</a> for this important topic.</p>\n<p>We will, however, take a look at some things specific to this course.</p>\n<p>The Open Web Application Security Project, otherwise known as <a href=\"https://www.owasp.org\">OWASP</a>, publishes an annual list of the most common security risks in Web applications. The most recent list can be found <a href=\"https://owasp.org/Top10/\">here</a>. The same risks can be found from one year to another.</p>\n<p>At the top of the list, we find <i>injection</i>, which means that e.g. text sent using a form in an application is interpreted completely differently than the software developer had intended. The most famous type of injection is probably <a href=\"https://stackoverflow.com/questions/332365/how-does-the-sql-injection-from-the-bobby-tables-xkcd-comic-work\">SQL injection</a>.</p>\n<p>For example, imagine that the following SQL query is executed in a vulnerable application:</p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">let</span> query <span class=\"token operator\">=</span> <span class=\"token string\">\"SELECT * FROM Users WHERE name = '\"</span> <span class=\"token operator\">+</span> userName <span class=\"token operator\">+</span> <span class=\"token string\">\"';\"</span></code></pre></div>\n<p>Now let's assume that a malicious user <i>Arto Hellas</i> would define their name as</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">Arto Hell-as'; DROP TABLE Users; --</code></pre></div>\n<p>so that the name would contain a single quote <code>'</code>, which is the beginning and end character of a SQL string. As a result of this, two SQL operations would be executed, the second of which would destroy the database table <i>Users</i>:</p>\n<div class=\"gatsby-highlight\" data-language=\"sql\"><pre class=\"language-sql\"><code class=\"language-sql\"><span class=\"token keyword\">SELECT</span> <span class=\"token operator\">*</span> <span class=\"token keyword\">FROM</span> Users <span class=\"token keyword\">WHERE</span> name <span class=\"token operator\">=</span> <span class=\"token string\">'Arto Hell-as'</span><span class=\"token punctuation\">;</span> <span class=\"token keyword\">DROP</span> <span class=\"token keyword\">TABLE</span> Users<span class=\"token punctuation\">;</span> <span class=\"token comment\">--'</span></code></pre></div>\n<p>SQL injections are prevented using <a href=\"https://security.stackexchange.com/questions/230211/why-are-stored-procedures-and-prepared-statements-the-preferred-modern-methods-f\">parameterized queries</a>. With them, user input isn't mixed with the SQL query, but the database itself inserts the input values at placeholders in the query (usually <code>?</code>):</p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token function\">execute</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"SELECT * FROM Users WHERE name = ?\"</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">[</span>userName<span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span></code></pre></div>\n<p>Injection attacks are also possible in NoSQL databases. However, mongoose prevents them by <a href=\"https://zanon.io/posts/nosql-injection-in-mongodb\">sanitizing</a> the queries. More on the topic can be found e.g. <a href=\"https://web.archive.org/web/20220901024441/https://blog.websecurify.com/2014/08/hacking-nodejs-and-mongodb.html\">here</a>.</p>\n<p><i>Cross-site scripting (XSS)</i> is an attack where it is possible to inject malicious JavaScript code into a legitimate web application. The malicious code would then be executed in the browser of the victim. If we try to inject the following into e.g. the notes application:</p>\n<div class=\"gatsby-highlight\" data-language=\"html\"><pre class=\"language-html\"><code class=\"language-html\"><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>script</span><span class=\"token punctuation\">></span></span><span class=\"token script\"><span class=\"token language-javascript\">\n  <span class=\"token function\">alert</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Evil XSS attack'</span><span class=\"token punctuation\">)</span>\n</span></span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>script</span><span class=\"token punctuation\">></span></span></code></pre></div>\n<p>the code is not executed, but is only rendered as 'text' on the page:</p>\n<picture><img src=\"/static/7fee25a58d613e21d229e3b3db6c61d0/5a190/32e.png\" alt=\"browser showing notes with XSS attempt\" srcset=\"/static/7fee25a58d613e21d229e3b3db6c61d0/772e8/32e.png 200w,\n/static/7fee25a58d613e21d229e3b3db6c61d0/e17e5/32e.png 400w,\n/static/7fee25a58d613e21d229e3b3db6c61d0/5a190/32e.png 800w,\n/static/7fee25a58d613e21d229e3b3db6c61d0/c1b63/32e.png 1200w,\n/static/7fee25a58d613e21d229e3b3db6c61d0/0d98f/32e.png 1276w\" sizes=\"(max-width: 800px) 100vw, 800px\"></picture>\n<p>since React <a href=\"https://legacy.reactjs.org/docs/introducing-jsx.html#jsx-prevents-injection-attacks\">takes care of sanitizing data in variables</a>. Some versions of React <a href=\"https://medium.com/dailyjs/exploiting-script-injection-flaws-in-reactjs-883fb1fe36c1\">have been vulnerable</a> to XSS attacks. The security holes have of course been patched, but there is no guarantee that there couldn't be any more.</p>\n<p>One needs to remain vigilant when using libraries; if there are security updates to those libraries, it is advisable to update those libraries in one's applications. Security updates for Express are found in the <a href=\"https://expressjs.com/en/advanced/security-updates.html\">library's documentation</a> and the ones for Node are found in <a href=\"https://nodejs.org/en/blog/vulnerability/\">this blog</a>.</p>\n<p>You can check how up-to-date your dependencies are using the command</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token function\">npm</span> outdated <span class=\"token parameter variable\">--depth</span> <span class=\"token number\">0</span></code></pre></div>\n<p>The one-year-old project that is used in <a href=\"/en/part9\">part 9</a> of this course already has quite a few outdated dependencies:</p>\n<picture><img src=\"/static/b199b453cdb888941aed98daf5c32b0a/5a190/33x.png\" alt=\"npm outdated output of patientor\" srcset=\"/static/b199b453cdb888941aed98daf5c32b0a/772e8/33x.png 200w,\n/static/b199b453cdb888941aed98daf5c32b0a/e17e5/33x.png 400w,\n/static/b199b453cdb888941aed98daf5c32b0a/5a190/33x.png 800w,\n/static/b199b453cdb888941aed98daf5c32b0a/c1b63/33x.png 1200w,\n/static/b199b453cdb888941aed98daf5c32b0a/29007/33x.png 1600w,\n/static/b199b453cdb888941aed98daf5c32b0a/3a1b1/33x.png 1686w\" sizes=\"(max-width: 800px) 100vw, 800px\"></picture>\n<p>The dependencies can be brought up to date by updating the file <i>package.json</i>. The best way to do that is by using a tool called <i>npm-check-updates</i>. It can be installed globally by running the command:</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token function\">npm</span> <span class=\"token function\">install</span> <span class=\"token parameter variable\">-g</span> npm-check-updates</code></pre></div>\n<p>Using this tool, the up-to-dateness of dependencies is checked in the following way:</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">$ npm-check-updates\nChecking <span class=\"token punctuation\">..</span>.<span class=\"token punctuation\">\\</span>my-app<span class=\"token punctuation\">\\</span>package.json\n<span class=\"token punctuation\">[</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token punctuation\">]</span> <span class=\"token number\">11</span>/11 <span class=\"token number\">100</span>%\n\n @testing-library/react       ^14.0.0  →  ^15.0.0\n @testing-library/user-event  ^14.4.3  →  ^14.5.2\n react                        ^18.2.0  →  ^19.0.0\n vite                          ^5.0.0  →   ^6.0.0\n\nRun ncu <span class=\"token parameter variable\">-u</span> to upgrade package.json</code></pre></div>\n<p>The file <i>package.json</i> is brought up to date by running the command <i>ncu -u</i>.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">$ ncu <span class=\"token parameter variable\">-u</span>\nUpgrading <span class=\"token punctuation\">..</span>.<span class=\"token punctuation\">\\</span>my-app<span class=\"token punctuation\">\\</span>package.json\n<span class=\"token punctuation\">[</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token punctuation\">]</span> <span class=\"token number\">11</span>/11 <span class=\"token number\">100</span>%\n\n @testing-library/react       ^14.0.0  →  ^15.0.0\n @testing-library/user-event  ^14.4.3  →  ^14.5.2\n react                        ^18.2.0  →  ^19.0.0\n vite                          ^5.0.0  →   ^6.0.0\n\nRun <span class=\"token function\">npm</span> <span class=\"token function\">install</span> to <span class=\"token function\">install</span> new versions.</code></pre></div>\n<p>Then it is time to update the dependencies by running the command <i>npm install</i>. However, old versions of the dependencies are not necessarily a security risk.</p>\n<p>The npm <a href=\"https://docs.npmjs.com/cli/audit\">audit</a> command can be used to check the security of dependencies. It compares the version numbers of the dependencies in your application to a list of the version numbers of dependencies containing known security threats in a centralized error database.</p>\n<p>Running <i>npm audit</i> on the same project, it prints a long list of complaints and suggested fixes.\nBelow is a part of the report:</p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\">$ patientor npm audit\n\n<span class=\"token operator\">...</span> many lines removed <span class=\"token operator\">...</span>\n\nurl<span class=\"token operator\">-</span>parse  <span class=\"token operator\">&lt;</span><span class=\"token number\">1.5</span><span class=\"token number\">.2</span>\n<span class=\"token literal-property property\">Severity</span><span class=\"token operator\">:</span> moderate\nOpen redirect <span class=\"token keyword\">in</span> url<span class=\"token operator\">-</span>parse <span class=\"token operator\">-</span> https<span class=\"token operator\">:</span><span class=\"token operator\">/</span><span class=\"token operator\">/</span>github<span class=\"token punctuation\">.</span>com<span class=\"token operator\">/</span>advisories<span class=\"token operator\">/</span><span class=\"token constant\">GHSA</span><span class=\"token operator\">-</span>hh27<span class=\"token operator\">-</span>ffr2<span class=\"token operator\">-</span>f2jc\nfix available via <span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">npm audit fix</span><span class=\"token template-punctuation string\">`</span></span>\nnode_modules<span class=\"token operator\">/</span>url<span class=\"token operator\">-</span>parse\n\nws  <span class=\"token number\">6.0</span><span class=\"token number\">.0</span> <span class=\"token operator\">-</span> <span class=\"token number\">6.2</span><span class=\"token number\">.1</span> <span class=\"token operator\">||</span> <span class=\"token number\">7.0</span><span class=\"token number\">.0</span> <span class=\"token operator\">-</span> <span class=\"token number\">7.4</span><span class=\"token number\">.5</span>\n<span class=\"token literal-property property\">Severity</span><span class=\"token operator\">:</span> moderate\nReDoS <span class=\"token keyword\">in</span> Sec<span class=\"token operator\">-</span>Websocket<span class=\"token operator\">-</span>Protocol header <span class=\"token operator\">-</span> https<span class=\"token operator\">:</span><span class=\"token operator\">/</span><span class=\"token operator\">/</span>github<span class=\"token punctuation\">.</span>com<span class=\"token operator\">/</span>advisories<span class=\"token operator\">/</span><span class=\"token constant\">GHSA</span><span class=\"token operator\">-</span>6fc8<span class=\"token operator\">-</span>4gx4<span class=\"token operator\">-</span>v693\nReDoS <span class=\"token keyword\">in</span> Sec<span class=\"token operator\">-</span>Websocket<span class=\"token operator\">-</span>Protocol header <span class=\"token operator\">-</span> https<span class=\"token operator\">:</span><span class=\"token operator\">/</span><span class=\"token operator\">/</span>github<span class=\"token punctuation\">.</span>com<span class=\"token operator\">/</span>advisories<span class=\"token operator\">/</span><span class=\"token constant\">GHSA</span><span class=\"token operator\">-</span>6fc8<span class=\"token operator\">-</span>4gx4<span class=\"token operator\">-</span>v693\nfix available via <span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">npm audit fix</span><span class=\"token template-punctuation string\">`</span></span>\nnode_modules<span class=\"token operator\">/</span>webpack<span class=\"token operator\">-</span>dev<span class=\"token operator\">-</span>server<span class=\"token operator\">/</span>node_modules<span class=\"token operator\">/</span>ws\nnode_modules<span class=\"token operator\">/</span>ws\n\n<span class=\"token number\">120</span> <span class=\"token function\">vulnerabilities</span> <span class=\"token punctuation\">(</span><span class=\"token number\">102</span> moderate<span class=\"token punctuation\">,</span> <span class=\"token number\">16</span> high<span class=\"token punctuation\">,</span> <span class=\"token number\">2</span> critical<span class=\"token punctuation\">)</span>\n\nTo address issues that <span class=\"token keyword\">do</span> not require attention<span class=\"token punctuation\">,</span> <span class=\"token literal-property property\">run</span><span class=\"token operator\">:</span>\n  npm audit fix\n\nTo address all <span class=\"token function\">issues</span> <span class=\"token punctuation\">(</span>including breaking changes<span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span> <span class=\"token literal-property property\">run</span><span class=\"token operator\">:</span>\n  npm audit fix <span class=\"token operator\">--</span>force</code></pre></div>\n<p>After only one year, the code is full of small security threats. Luckily, there are only 2 critical threats.  Let's run <i>npm audit fix</i> as the report suggests:</p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\">$ npm audit fix\n\n<span class=\"token operator\">+</span> mongoose@<span class=\"token number\">5.9</span><span class=\"token number\">.1</span>\nadded <span class=\"token number\">19</span> packages from <span class=\"token number\">8</span> contributors<span class=\"token punctuation\">,</span> removed <span class=\"token number\">8</span> packages and updated <span class=\"token number\">15</span> packages <span class=\"token keyword\">in</span> <span class=\"token number\">7</span><span class=\"token punctuation\">.</span>325s\nfixed <span class=\"token number\">354</span> <span class=\"token keyword\">of</span> <span class=\"token number\">416</span> vulnerabilities <span class=\"token keyword\">in</span> <span class=\"token number\">20047</span> scanned packages\n  <span class=\"token number\">1</span> <span class=\"token keyword\">package</span> update <span class=\"token keyword\">for</span> <span class=\"token number\">62</span> vulns involved breaking <span class=\"token function\">changes</span>\n  <span class=\"token punctuation\">(</span>use <span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">npm audit fix --force</span><span class=\"token template-punctuation string\">`</span></span> to install breaking changes<span class=\"token punctuation\">;</span> or refer to <span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">npm audit</span><span class=\"token template-punctuation string\">`</span></span> <span class=\"token keyword\">for</span> steps to fix these manually<span class=\"token punctuation\">)</span></code></pre></div>\n<p>62 threats remain because, by default, <i>audit fix</i> does not update dependencies if their <i>major</i> version number has increased.  Updating these dependencies could lead to the whole application breaking down.</p>\n<p>The source for the critical bug is the library <a href=\"https://github.com/immerjs/immer\">immer</a></p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\">immer  <span class=\"token operator\">&lt;</span><span class=\"token number\">9.0</span><span class=\"token number\">.6</span>\n<span class=\"token literal-property property\">Severity</span><span class=\"token operator\">:</span> critical\nPrototype Pollution <span class=\"token keyword\">in</span> immer <span class=\"token operator\">-</span> https<span class=\"token operator\">:</span><span class=\"token operator\">/</span><span class=\"token operator\">/</span>github<span class=\"token punctuation\">.</span>com<span class=\"token operator\">/</span>advisories<span class=\"token operator\">/</span><span class=\"token constant\">GHSA</span><span class=\"token operator\">-</span>33f9<span class=\"token operator\">-</span>j839<span class=\"token operator\">-</span>rf8h\nfix available via <span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">npm audit fix --force</span><span class=\"token template-punctuation string\">`</span></span>\nWill install react<span class=\"token operator\">-</span>scripts@<span class=\"token number\">5.0</span><span class=\"token number\">.0</span><span class=\"token punctuation\">,</span> which is a breaking change</code></pre></div>\n<p>Running <i>npm audit fix --force</i> would upgrade the library version but would also upgrade the library <i>react-scripts</i> and that would potentially break down the development environment. So we will leave the library upgrades for later...</p>\n<p>One of the threats mentioned in the list from OWASP is <i>Broken Authentication</i> and the related <i>Broken Access Control</i>. The token-based authentication we have been using is fairly robust if the application is being used on the traffic-encrypting HTTPS protocol. When implementing access control, one should e.g. remember to not only check a user's identity in the browser but also on the server. Bad security would be to prevent some actions to be taken only by hiding the execution options in the code of the browser.</p>\n<p>On Mozilla's MDN, there is a very good <a href=\"https://developer.mozilla.org/en-US/docs/Learn/Server-side/First_steps/Website_security\">Website security guide</a>, which brings up this very important topic:</p>\n<picture><img src=\"/static/a7b95baf708b2945f316155b024c062d/5a190/34.png\" alt=\"screenshot of website security from MDN\" srcset=\"/static/a7b95baf708b2945f316155b024c062d/772e8/34.png 200w,\n/static/a7b95baf708b2945f316155b024c062d/e17e5/34.png 400w,\n/static/a7b95baf708b2945f316155b024c062d/5a190/34.png 800w,\n/static/a7b95baf708b2945f316155b024c062d/c1b63/34.png 1200w,\n/static/a7b95baf708b2945f316155b024c062d/52576/34.png 1412w\" sizes=\"(max-width: 800px) 100vw, 800px\"></picture>\n<p>The documentation for Express includes a section on security: <a href=\"https://expressjs.com/en/advanced/best-practice-security.html\">Production Best Practices: Security</a>, which is worth a read. It is also recommended to add a library called <a href=\"https://helmetjs.github.io/\">Helmet</a> to the backend. It includes a set of middleware that eliminates some security vulnerabilities in Express applications.</p>\n<p>Using the ESlint <a href=\"https://github.com/nodesecurity/eslint-plugin-security\">security-plugin</a> is also worth doing.</p>\n<h3>Current trends</h3>\n<p>Finally, let's take a look at some technology of tomorrow (or, actually, already today), and the directions in which Web development is heading.</p>\n<h4>Typed versions of JavaScript</h4>\n<p>The <a href=\"https://developer.mozilla.org/en-US/docs/Glossary/Dynamic_typing\">dynamic typing</a> of JavaScript can lead to subtle bugs that are only discovered at runtime. In part 5, we touched on <a href=\"/en/part5/props_children_and_proptypes#prop-types\">PropTypes</a> as a way to add runtime type checks to component props, but PropTypes have largely fallen out of use as the ecosystem has moved toward <a href=\"https://en.wikipedia.org/wiki/Type_system#Static_type_checking\">static type checking</a>.</p>\n<p><a href=\"https://www.typescriptlang.org/\">TypeScript</a>, developed by Microsoft, has become the de facto standard for typed JavaScript. It catches type errors at compile time rather than at runtime, provides excellent editor tooling, and is now used by the majority of new React projects. TypeScript is covered in <a href=\"/en/part9\">part 9</a>.</p>\n<h4>Server-side rendering and React Server Components</h4>\n<p>React components do not have to run in the browser. They can also be rendered on the <a href=\"https://react.dev/reference/react-dom/server\">server</a>, which sends ready-made HTML to the client instead of a blank page that JavaScript must fill in. This <i>server-side rendering</i> (SSR) improves perceived load time and is important for Search Engine Optimization (SEO), since search engine crawlers see fully rendered content without having to execute JavaScript.</p>\n<p>The more recent and significant development is <a href=\"https://react.dev/blog/2023/03/22/react-labs-what-we-have-been-working-on-march-2023#react-server-components\">React Server Components</a> (RSC), introduced in React 18 and now a core part of the React architecture. A Server Component runs exclusively on the server and is never sent to the browser as JavaScript. It can read directly from a database or file system, keep secrets out of the client bundle, and stream its output to the browser. The browser receives these components as rendered data, not as executable code. <i>Client Components</i>, annotated with <code>'use client'</code>, still run in the browser and handle interactivity as before. In an RSC application most components are Server Components by default, with Client Components used only where user interaction is needed.</p>\n<p><a href=\"https://nextjs.org/\">Next.js</a> has become the standard framework for building React applications that require server-side behaviour. Its App Router (introduced in Next.js 13) is built around React Server Components and provides file-based routing, nested layouts, server actions for mutating data, and built-in support for static generation and incremental static regeneration. In 2026, Next.js is the first choice for any React project where SSR, SEO, or full-stack capabilities matter.</p>\n<h4>Microservice architecture</h4>\n<p>During this course, we have only scratched the surface of the server end of things. In our applications, we had a <i>monolithic</i> backend, meaning one application making up a whole and running on a single server, serving only a few API endpoints.</p>\n<p>As the application grows, the monolithic backend approach starts turning problematic both in terms of performance and maintainability.</p>\n<p>A <a href=\"https://martinfowler.com/articles/microservices.html\">microservice architecture</a> (microservices) is a way of composing the backend of an application from many separate, independent services, which communicate with each other over the network. An individual microservice's purpose is to take care of a particular logical functional whole. In a pure microservice architecture, the services do not use a shared database.</p>\n<p>For example, the bloglist application could consist of two services: one handling the user and another taking care of the blogs. The responsibility of the user service would be user registration and user authentication, while the blog service would take care of operations related to the blogs.</p>\n<p>The image below visualizes the difference between the structure of an application based on a microservice architecture and one based on a more traditional monolithic structure:</p>\n<picture><img src=\"/static/beecf1d05714ef6a4ac0721fce62d394/5a190/36.png\" alt=\"microservices vs traditional approach diagram\" srcset=\"/static/beecf1d05714ef6a4ac0721fce62d394/772e8/36.png 200w,\n/static/beecf1d05714ef6a4ac0721fce62d394/e17e5/36.png 400w,\n/static/beecf1d05714ef6a4ac0721fce62d394/5a190/36.png 800w,\n/static/beecf1d05714ef6a4ac0721fce62d394/c1b63/36.png 1200w,\n/static/beecf1d05714ef6a4ac0721fce62d394/ce0a7/36.png 1590w\" sizes=\"(max-width: 800px) 100vw, 800px\"></picture>\n<p>The role of the frontend (enclosed by a square in the picture) does not differ much between the two models. There is often a so-called <a href=\"http://microservices.io/patterns/apigateway\">API gateway</a> between the microservices and the frontend, which provides an illusion of a more traditional \"everything on the same server\" API. <a href=\"https://medium.com/netflix-techblog/optimizing-the-netflix-api-5c9ac715cf19\">Netflix</a>, among others, uses this type of approach.</p>\n<p>Microservice architectures emerged and evolved for the needs of large internet-scale applications. The trend was set by Amazon far before the appearance of the term microservice. The critical starting point was an email sent to all employees in 2002 by Amazon CEO Jeff Bezos:</p>\n<blockquote>\n<p>All teams will henceforth expose their data and functionality through service interfaces.</p>\n<p>Teams must communicate with each other through these interfaces.</p>\n<p>There will be no other form of inter-process communication allowed: no direct linking, no direct reads of another team’s data store, no shared-memory model, no back-doors whatsoever. The only communication allowed is via service interface calls over the network.</p>\n<p>It doesn’t matter what technology you use.</p>\n<p>All service interfaces, without exception, must be designed from the ground up to be externalize-able. That is to say, the team must plan and design to be able to expose the interface to developers in the outside world.</p>\n<p>No exceptions.</p>\n<p>Anyone who doesn’t do this will be fired. Thank you; have a nice day!</p>\n</blockquote>\n<p>Nowadays, one of the biggest forerunners in the use of microservices is <a href=\"https://www.infoq.com/presentations/netflix-chaos-microservices\">Netflix</a>.</p>\n<p>The use of microservices has steadily been gaining hype to be kind of a <a href=\"https://en.wikipedia.org/wiki/No_Silver_Bullet\">silver bullet</a> of today, which is being offered as a solution to almost every kind of problem. However, there are several challenges when it comes to applying a microservice architecture, and it might make sense to go <a href=\"https://martinfowler.com/bliki/MonolithFirst.html\">monolith first</a> by initially making a traditional all-encompassing backend. Or maybe <a href=\"https://martinfowler.com/articles/dont-start-monolith.html\">not</a>. There are a bunch of different opinions on the subject. Both links lead to Martin Fowler's site; as we can see, even the wise are not entirely sure which one of the right ways is more right.</p>\n<p>Unfortunately, we cannot dive deeper into this important topic during this course. Even a cursory look at the topic would require at least 5 more weeks.</p>\n<h4>Serverless</h4>\n<p>After the release of Amazon's <a href=\"https://aws.amazon.com/lambda/\">lambda</a> service at the end of 2014, a new trend started to emerge in web application development: <a href=\"https://serverless.com/\">serverless</a>.</p>\n<p>The main thing about lambda, and nowadays also Google's <a href=\"https://cloud.google.com/functions/\">Cloud functions</a> as well as <a href=\"https://azure.microsoft.com/en-us/services/functions/\">similar functionality in Azure</a>, is that it enables <i>the execution of individual functions</i> in the cloud. Before, the smallest executable unit in the cloud was a single <i>process</i>, e.g. a runtime environment running a Node backend.</p>\n<p>E.g. Using Amazon's <a href=\"https://aws.amazon.com/api-gateway/\">API gateway</a> it is possible to make serverless applications where the requests to the defined HTTP API get responses directly from cloud functions. Usually, the functions already operate using stored data in the databases of the cloud service.</p>\n<p>Serverless is not about there not being a server in applications, but about how the server is defined. Software developers can shift their programming efforts to a higher level of abstraction as there is no longer a need to programmatically define the routing of HTTP requests, database relations, etc., since the cloud infrastructure provides all of this. Cloud functions also lend themselves to creating a well-scaling system, e.g. Amazon's Lambda can execute a massive amount of cloud functions per second. All of this happens automatically through the infrastructure and there is no need to initiate new servers, etc.</p>\n<h3>Useful libraries and further reading</h3>\n<p>The JavaScript developer community has produced a large variety of useful libraries. Before writing something from scratch it is always worth checking whether a well-maintained solution already exists.</p>\n<p>You can take advantage of your React know-how when developing mobile applications using <a href=\"https://reactnative.dev/\">React Native</a>, which is the topic of <a href=\"/en/part10\">part 10</a> of the course.</p>\n<p>The course itself continues beyond part 7: <a href=\"/en/part8\">part 8</a> covers GraphQL, <a href=\"/en/part9\">part 9</a> TypeScript, <a href=\"/en/part10\">part 10</a> React Native, <a href=\"/en/part11\">part 11</a> CI/CD, and <a href=\"/en/part12\">part 12</a> containers. The full course contents are listed on the <a href=\"/en/#course-contents\">course page</a>.</p>\n<p>The following external resources are good places to go deeper on React patterns, code quality, and the broader ecosystem:</p>\n<ul>\n<li><a href=\"https://www.patterns.dev/\">Patterns.dev</a> covers modern React and JavaScript patterns in depth. For a curated collection of React-specific techniques, <a href=\"https://vasanthk.gitbooks.io/react-bits/\">React bits</a> is a useful companion.</li>\n<li><a href=\"https://overreacted.io/\">Overreacted</a> is the blog of Dan Abramov, one of the original React core team members. The articles go deep into React's design decisions and mental models, and are worth reading even when they are a few years old.</li>\n<li><a href=\"https://kentcdodds.com/blog\">Kent C. Dodds</a> writes extensively about React best practices, testing, and component design. His posts on testing philosophy in particular have shaped how the community thinks about frontend tests.</li>\n<li><a href=\"https://alexkondov.com/tao-of-react/\">Tao of React</a> is a short, opinionated guide to structuring React applications that covers components, state, props, and project layout in a pragmatic way.</li>\n<li><a href=\"https://www.reactiflux.com/\">Reactiflux</a> is a large React developer community on Discord, and a good place to ask questions after the course ends. Many open-source libraries maintain their own channels there.</li>\n</ul>\n</div>","frontmatter":{"mainImage":{"publicURL":"/static/2184b1c35920cfc7a09e10660dea04eb/part-7.svg"},"part":7,"letter":"c","lang":"en"}}},"pageContext":{"part":7,"letter":"c","lang":"en"}},"staticQueryHashes":["3128451518"]}