<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Charles Oroko on Medium]]></title>
        <description><![CDATA[Stories by Charles Oroko on Medium]]></description>
        <link>https://medium.com/@ogamba.co?source=rss-5d545b759c27------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*nzu1jkZ1K8KfmAlDOhqayw.jpeg</url>
            <title>Stories by Charles Oroko on Medium</title>
            <link>https://medium.com/@ogamba.co?source=rss-5d545b759c27------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 07:32:49 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ogamba.co/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to create a web app with React, Koa, Webpack, and MySQL: Part 1 (Backend)]]></title>
            <link>https://medium.com/@ogamba.co/how-to-create-a-web-app-with-react-koa-webpack-and-mysql-part-1-backend-e047eb3bbae2?source=rss-5d545b759c27------2</link>
            <guid isPermaLink="false">https://medium.com/p/e047eb3bbae2</guid>
            <category><![CDATA[koajs]]></category>
            <category><![CDATA[backend-development]]></category>
            <dc:creator><![CDATA[Charles Oroko]]></dc:creator>
            <pubDate>Sat, 27 Jun 2020 04:15:22 GMT</pubDate>
            <atom:updated>2020-06-27T04:15:22.465Z</atom:updated>
            <content:encoded><![CDATA[<h3>The Premise</h3><p>When I started to get into web design, I was directed to use <a href="https://koajs.com/">KoaJS</a>, and <a href="https://reactjs.org/">React</a>. Koa for its modular, lightweight design, and React because it was an industry standard. I struggled a lot in the beginning, partly due to my ignorance, and partly due to a lack of appropriate documentation or tutorials aimed at beginners.</p><p>Therefore, The aim of this tutorial is to show how to create a simple app from scratch using KoaJS and React. The app that I will be making to do this is a diary. In this part, I will be designing and implementing the backend. I will be assuming that the reader has at least a working knowledge of <strong>JavaScript </strong>and <strong>Node.js</strong>. The files for this project can be found at the following repository.</p><p><a href="https://github.com/chiq2045/my-diary">chiq2045/my-diary</a></p><h3>The App</h3><p>Let us define what we will be designing. A diary is a collection of entries that a user makes on specific dates. In order to create one, we need to facilitate the creation and manipulation of entries and the selection of dates. This calls for the design of a <a href="https://restfulapi.net/">REST API</a> to allow access to the app’s resources. In addition, I would like to connect each entry to a specific user’s diary, so we should create one for users too.</p><p>We should also consider what data users and entries should have. Each user should have an email address, password, and name. Each entry should have a date, user, and entry data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/490/1*gDT95EKfrc96suOb1IzPow.png" /></figure><h3>Setting up the backend</h3><p>The relevant directory listing for this part of the tutorial is shown below.</p><pre>my-diary/<br>+-- backend<br>|   +-- entries<br>|   |   +-- index.js<br>|   +-- users<br>|   |   +-- index.js<br>|   +-- server.js<br>|   +-- .eslintrc.json<br>+-- package-lock.json<br>+-- package.json<br>+-- README.md</pre><h4>Install the packages</h4><p>I like using npm as my package manager, but the reader can use the package manager they like.</p><pre>npm i koa koa-mount @koa/router koa-logger koa-body<br>npm i -D nodemon</pre><ul><li>koa — node framework used to design backend</li><li><a href="https://www.npmjs.com/package/koa-mount">koa-mount</a> — used to “mount” users and entries applications on the server</li><li><a href="https://www.npmjs.com/package/@koa/router">koa/router</a> — used to design routes for the APIs</li><li><a href="https://www.npmjs.com/package/koa-logger">koa-logger</a> — used to log data to console or selected file</li><li><a href="https://www.npmjs.com/package/koa-body">koa-body</a> — used to parse request bodies</li><li><a href="https://www.npmjs.com/package/nodemon">nodemon</a> — used to watch files and run backend server</li></ul><h4>Create a simple server</h4><p>Now we can begin by creating a simple server. Create the server file in the backend directory and copy and paste the following code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/721864d8813d77a692942f1b112bf4ca/href">https://medium.com/media/721864d8813d77a692942f1b112bf4ca/href</a></iframe><p>What this does is create an instance of the Koa object app and run the server on port 1337. It also returns logs on the console.</p><p>Now add a script to the package.json file that will run the server: (line 7)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5c56c95f1b99e39ace1f88dcb5cd64a2/href">https://medium.com/media/5c56c95f1b99e39ace1f88dcb5cd64a2/href</a></iframe><p>The server can be run using the dev:backend command npm run dev:backend</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/517/1*e691O47Xx2Q06jyTdbfjhw.png" /></figure><p>Click on the link to navigate to the server. Seeing as the server is empty you will get a “404 Not Found” response. This is because the server is not returning anything. Now that we know that the server is running, we can leave nodemon running in the terminal. nodemon watches for changes and resets the server as they are made so you do not need to reset it.</p><h3>Creating the REST API</h3><h4>What is REST API</h4><p>A REST API consists of routes that allow for complex interactions within systems. These routes will be reached using four <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods">HTTP mehods</a> (GET, POST, PUT, DELETE). Though there are more methods, these four will do everything we need at this level.</p><ul><li>GET — requests one or more resource(s) from the server</li><li>POST — requests the server to create a new resource</li><li>PUT — requests the server to update a given resource (or creates one if it does not exist)</li><li>DELETE — requests the server to delete a given resource</li></ul><p>Each route will allow us to hit an endpoint that corresponds to one of these methods. Seeing as we have two main API routes, users and entries, let us create directories for each of these in a routes folder. In here we will place all of the routes and logic that pertains to them.</p><pre>mkdir backend/routes/users &amp;&amp; mkdir backend/routes/entries</pre><h4>Creating users routes</h4><p>Create an index file for the users route:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3fe353f2366cc5570353eaf25499fa12/href">https://medium.com/media/3fe353f2366cc5570353eaf25499fa12/href</a></iframe><p>This will create a Koa instance users and a koa-router instance userRouter that has five routes. At the moment, koaBody is not being used but we will use it soon.</p><p>Note that lines 28 and 29 specify that we are adding some middleware to the users app. In this case, we are adding the userRouter routes and it’s alowed http methods. userRouter.routes() sends a route matching the given request. userRouter.allowedMethods() responds to OPTIONS requests with an Allow header and can give other responses as are appropriate.</p><p>Also, note the use of ctx. It is an object that wraps node’s request and response object. Reading the <a href="https://koajs.com/">docs</a> will give more “Context”. We will discuss this later when we actually do use the context to interact with the request and response objects.</p><p>Now open server.js and mount the users app to the users route http://localhost:1337/users. Notice the use of path.resolve() in order to always get the absolute path of the file. While it is not necessary, I have come across some select situations where things did not work without the absolute paths, so this has become habit in order to lessen debugging.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2a2707486e82c2bc080ca8a1a168513e/href">https://medium.com/media/2a2707486e82c2bc080ca8a1a168513e/href</a></iframe><p>Finally navigate to the users route in the server and test each of the routes. I am using <a href="https://www.postman.com/">Postman</a> to do this: <a href="http://localhost:1337/users">http://localhost:1337/users</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/406/1*SAFW2ulj5vvrM-c0LGVORA.png" /></figure><p>We got a response on the console, but we are still hitting a 404. That is because the server is not sending anything back on any of the routes. Let us fix that.</p><p>Open the users index file and update the routes to return some message.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0d7140008a38468935998678a0c6e795/href">https://medium.com/media/0d7140008a38468935998678a0c6e795/href</a></iframe><p>We have used koaBody in the post route. Passing this middleware allows us to parse request bodies for data. If we do not pass it the server will throw an error. Try it for yourself!</p><p>We should also discuss the ctx object again. We will do this by looking at the post route again. The post route parses the response object by referencing the context and the request body, (line 21). It also sends data back using the context response body, (lines 29–36). This is a very important issue to understand.</p><p>Now navigate to the users routes again. We no longer have a 404 Not found, and are able to send messages from the backend!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/401/1*ON8dn71ThEkSJi0tKtH2CQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/548/1*dmIJJFfE5UNKDqccUey41A.png" /></figure><h4>Creating the entries routes</h4><p>Now that we can hit the API endpoints forThe users, we can do the same for the entries API.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8d7e00d9603ce0336dd8ec4186ea7cf8/href">https://medium.com/media/8d7e00d9603ce0336dd8ec4186ea7cf8/href</a></iframe><p>The entries API is similar to the users API. This is not a bad thing. APIs used for the same app should have a simple, visible and uniform interface.</p><p>Now, mount the entries app on the server and test the API:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8e1f0098a1341de0443c902b8430abff/href">https://medium.com/media/8e1f0098a1341de0443c902b8430abff/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/470/1*rlQn3EBNQpDbvvtVsYrjBQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/519/1*hJImGYvQaNJLtZsyoeo1dQ.png" /></figure><h3>Success!</h3><p>We have created a backend for the app that offers a REST API for both users and entries. At this point, it does nothing, but we will be dealing with that in Part 2, where we will be adding a database, and interacting with it.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e047eb3bbae2" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>