<?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 John O. Paul on Medium]]></title>
        <description><![CDATA[Stories by John O. Paul on Medium]]></description>
        <link>https://medium.com/@johnopaul?source=rss-6d678a7222de------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*J6P3rewMxoKc_YSR.</url>
            <title>Stories by John O. Paul on Medium</title>
            <link>https://medium.com/@johnopaul?source=rss-6d678a7222de------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 11:01:21 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@johnopaul/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[Structuring an Express.js API.]]></title>
            <link>https://medium.com/the-andela-way/structuring-an-express-js-api-fc62efa038c5?source=rss-6d678a7222de------2</link>
            <guid isPermaLink="false">https://medium.com/p/fc62efa038c5</guid>
            <category><![CDATA[expressjs]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[the-andela-way]]></category>
            <dc:creator><![CDATA[John O. Paul]]></dc:creator>
            <pubDate>Fri, 15 Feb 2019 09:54:08 GMT</pubDate>
            <atom:updated>2019-02-15T09:54:08.874Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VuBfuMpV8hiienrdARLyJQ.jpeg" /><figcaption>Photo credit <a href="https://unsplash.com/photos/P_qvsF7Yodw">https://unsplash.com/photos/P_qvsF7Yodw</a></figcaption></figure><p>This tutorial is a follow-up tutorial for the tutorial I wrote on <a href="https://medium.com/the-andela-way/how-to-set-up-an-express-api-using-webpack-and-typescript-69d18c8c4f52">How to set up an Express.js API using Webpack and TypeScript</a>.</p><p>The tutorial above covered how to set up the project, however, this one will cover how to structure the API in a way that allows for adding new versions of the API, good maintainability and clean code.</p><h3><strong>Prerequisites.</strong></h3><ul><li>Node.js, Yarn or npm.</li><li>Knowledge of Express.js</li><li>This is a follow-up tutorial, so I will use <a href="https://github.com/john555/express-app-typescript">code</a> from the previous tutorial.</li></ul><p>Here is the <a href="https://github.com/john555/express-app-typescript/tree/structure">repo</a> for the codebase for following along. The code for this tutorial is on the structure branch.</p><h3><strong>Steps.</strong></h3><ol><li>Clone this repo and install dependencies.</li></ol><pre>git clone <a href="https://github.com/john555/express-app-typescript.git">https://github.com/john555/express-app-typescript.git</a><br>cd express-app-typescript<br>yarn install # or npm install</pre><p>2. Start the app in dev mode using yarn start:dev.</p><p>3. Inside the src/ folder create a folder with the name api.</p><p>4. Inside the api/ folder create an index.ts file and a folder for controllers and routes. The directory structure for the src folder should look as shown below.</p><pre>.<br>+- src<br>  +- api<br>    +- controllers/<br>    +- routes/<br>    -- index.ts<br>  -- index.ts</pre><p>5. Create an express app in src/api/index.ts as shown below.</p><pre>import * as express from &#39;express&#39;;</pre><pre>const api = express();</pre><pre>// You may add api specific middlewares here</pre><pre>// TODO: move all controllers in the src/api/controllers folder<br>api.get(&#39;/&#39;, (req, res) =&gt; {<br>  res.send({<br>    message: &#39;Hello from the API&#39;,<br>  });<br>});</pre><pre>export default api;</pre><p>6. In src/index.ts, import the api above add it as a middleware scoped to all routes that start with ‘/api/v1’ as shown below.</p><pre>...<br>import api from &#39;./api&#39;;</pre><pre>const app = express();<br>...<br>app.use(&#39;/api/v1&#39;, api);</pre><p>Ensure the app is running. Open your browser and got to <a href="http://localhost:3000/api/v1/">http://localhost:3000/api/v1/</a>. You should see the following JSON response.</p><pre>{&quot;message&quot;:&quot;Hello from the API&quot;}</pre><p>Now, Let’s place routes and controllers in separate folders.</p><p>7. Create a file named getMovies.ts in /src/api/controllers/ with the following code.</p><pre>import { Request, Response } from &#39;express&#39;;</pre><pre>export function getMovies(req: Request, res: Response) {<br>  res.send({<br>    users:[<br>    {id: 1, name: &#39;How to train your dragon&#39; },<br>    {id: 2, name: &#39;Queen of Katwe&#39;},<br>    ]<br>  });<br>}</pre><p>8. Create a file named movieRouter.ts in /src/api/routes/ with the following contents.</p><pre>import { Router } from &#39;express&#39;;<br>import { getMovies } from &#39;../controllers/getMovies&#39;;</pre><pre>const movieRouter = Router();</pre><pre>movieRouter.get(&#39;/&#39;, getMovies);</pre><pre>export default movieRouter;</pre><p>9. Now, let’s add the movie router above as middleware to our api in src/api/index.ts while replacing the existing route. Below is how the file should look like.</p><pre>import * as express from &#39;express&#39;;</pre><pre>import movieRouter from &#39;./routes/movieRouter&#39;;</pre><pre>const api = express();</pre><pre>// You may add api specific middlewares here<br>// TODO: move all controllers in the src/api/controllers folder</pre><pre>// api.get(&#39;/&#39;, (req, res) =&gt; {<br>//   res.send({<br>//     message: &#39;Hello from the API&#39;,<br>//   });<br>// });</pre><pre>api.use(&#39;/movies&#39;, movieRouter)</pre><pre>export default api;</pre><p>10. Go to <a href="http://localhost:3000/api/v1/movies">http://localhost:3000/api/v1/movies</a> in your browser. You should see a response with movies.</p><p>11. Let’s restructure our routes such that we can import them like:</p><pre>import { movieRouter } from &#39;./routes/&#39;;</pre><ul><li>Create a file named index.ts in src/api/routes/ with the following code.</li></ul><pre>export * from &#39;./movieRouter&#39;;</pre><ul><li>Inside src/api/routes/movieRouter.ts, convert the export for movieRouter to a named export.</li></ul><pre>...<br>export const movieRouter = Router();<br>...</pre><ul><li>Go to src/api/index.ts and change how we’re importing the movieRouter to:</li></ul><pre>...<br>import { movieRouter } from &#39;./routes/&#39;;<br>...</pre><p>Go to <a href="http://localhost:3000/api/v1/movies">http://localhost:3000/api/v1/movies</a> in your browser. You should still see a response with movies.</p><p>That’s it!</p><h4>Final thoughts.</h4><p>Any middlewares like body-parser, loggers etc that would potentially be used in all versions of the API (or other kinda apps that you may want to add) should be added to the app instance in src/index.ts.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fc62efa038c5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-andela-way/structuring-an-express-js-api-fc62efa038c5">Structuring an Express.js API.</a> was originally published in <a href="https://medium.com/the-andela-way">The Andela Way</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to set up an Express API using Webpack and Typescript.]]></title>
            <link>https://medium.com/the-andela-way/how-to-set-up-an-express-api-using-webpack-and-typescript-69d18c8c4f52?source=rss-6d678a7222de------2</link>
            <guid isPermaLink="false">https://medium.com/p/69d18c8c4f52</guid>
            <category><![CDATA[webpack]]></category>
            <category><![CDATA[expressjs]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[typescript]]></category>
            <dc:creator><![CDATA[John O. Paul]]></dc:creator>
            <pubDate>Thu, 14 Feb 2019 14:49:18 GMT</pubDate>
            <atom:updated>2020-02-05T07:52:13.630Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to set up an Express.js API using Webpack and TypeScript.</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FhRHwniPmOSQIUgukNqH-Q.jpeg" /><figcaption>Photo from <a href="https://unsplash.com/photos/95YRwf6CNw8">https://unsplash.com/photos/95YRwf6CNw8</a></figcaption></figure><h3>Prerequisites.</h3><ul><li>Basic <a href="https://www.typescriptlang.org/">typescript</a> and <a href="https://webpack.js.org/">webpack</a> knowledge</li><li><a href="https://yarnpkg.com/en/docs/install">Yarn</a> or npm. In this tutorial, I will use yarn.</li><li><a href="https://nodejs.org/en/">Node.js</a></li><li>Code editor of your choice.</li></ul><p>Here is the link to the <a href="https://github.com/john555/express-app-typescript">repo</a> with the finished setup in case you get stuck.</p><h3>Setting up the App.</h3><ol><li>Open your terminal and create a new folder for your application. I will use the name express-app for this tutorial, but you can use any name.</li></ol><pre>mkdir express-app<br>cd express-app</pre><p>2. Create a package.json file in the folder above by running yarn init -y. I used the -y flag to accept the default yarn options, but you can .</p><p>3. Create a src/ folder and inside that, create an index.ts file. This is the entry file of the application.</p><p>4. Create a webpack.config.js file in the express-app folder and add the following config.</p><pre>const path = require(&#39;path&#39;);</pre><pre>const {<br>  NODE_ENV = &#39;production&#39;,<br>} = process.env;</pre><pre>module.exports = {<br>  entry: &#39;./src/index.ts&#39;,<br>  mode: NODE_ENV,<br>  target: &#39;node&#39;,<br>  output: {<br>    path: path.resolve(__dirname, &#39;build&#39;),<br>    filename: &#39;index.js&#39;<br>  },<br>  resolve: {<br>    extensions: [&#39;.ts&#39;, &#39;.js&#39;],<br>  }<br>}</pre><p>5. Add a build script in the package.js file.</p><pre>...<br>&quot;scripts&quot;: {<br>  &quot;build&quot;: &quot;webpack&quot;<br>}</pre><p>Run theyarn build command in the terminal as a smoke test for the setup.</p><p>6. Install express and it’s type definition files (@types/express) using yarn add express @types/expressand create a simple express app in src/index.ts as shown below. Type definition files enable us to enjoy the benefits of type checking.</p><pre>import * as express from &#39;express&#39;;<br>import { Request, Response } from &#39;express&#39;;</pre><pre>const app = express();</pre><pre>const {<br>  PORT = 3000,<br>} = process.env;</pre><pre>app.get(&#39;/&#39;, (req: Request, res: Response) =&gt; {<br>  res.send({<br>    message: &#39;hello world&#39;,<br>  });<br>});</pre><pre>app.listen(PORT, () =&gt; {<br>  console.log(&#39;server started at <a href="http://localhost:&#39;+PORT);">http://localhost:&#39;+PORT);</a><br>});</pre><p>The file above cannot be built by webpack out of the box. We need to add ts-loader to enable webpack to build it successfully.</p><p>7. Run yarn add ts-loader typescript --dev. The package ts-loader depends on thetypescript package. These are both development dependencies.</p><p>8. Add a typescript config file — tsconfig.json in the project root folder and add the following.</p><pre>{<br>  &quot;compilerOptions&quot;: {<br>    &quot;sourceMap&quot;: true<br>  }<br>}</pre><p>9. Add the following webpack config to the webpack.config.js file.</p><pre>...</pre><pre>module.exports = {<br>  ...<br>  module: {<br>    rules: [<br>      {<br>        test: /\.ts$/,<br>        use: [<br>          &#39;ts-loader&#39;,<br>        ]<br>      }<br>    ]<br>  }<br>}</pre><p>10. Run yarn build to test the build. This yields an error that says “Cannot find name ‘process’”. We can fix this by adding type definition files for node.js using yarn add @types/node --dev.</p><p>11. Optimization. If you open build/index.js , the first thing you notice is that the file is too large. That’s because some dependencies — packages in the node_modules folder are being added in the bundle. This makes the build process slow.</p><p>To fix this, we shall use Webpack’s <a href="https://webpack.js.org/configuration/externals/">externals</a> config option. This allows us to exclude some packages from the bundle.</p><p>Run yarn add webpack-node-externals --dev and add the following config to webpack.config.js</p><pre>const nodeExternals = require(&#39;webpack-node-externals&#39;);</pre><pre>module.exports = {<br>  ...<br>  externals: [ nodeExternals() ]<br>}</pre><p>Run yarn build . The build/index.js file should be much smaller in size and the build time should be significantly reduced.</p><p>12. Run the application using node build/index.js . And navigate to <a href="http://localhost:3000/">http://localhost:3000</a> in your web browser.</p><p>13. Improvement. If you are developing an app with this setup, you may want to have a setup where the app automatically reloads on save. Let’s leverage Webpack’s <a href="https://webpack.js.org/configuration/watch/">watch</a> config option as shown below.</p><pre>module.exports = {<br>  ...<br>  watch: NODE_ENV === &#39;development&#39;<br>}</pre><p>Add a start:dev script in package.json.</p><pre>...<br>&quot;scripts&quot;: {<br>  ...<br>  &quot;start:dev&quot;: &quot;NODE_ENV=development webpack&quot;,<br>}</pre><p>Run yarn start:dev . With NODE_ENV set to developement , webpack is watching our app files and rebuilding on saving.</p><p>Next, we need to run the app whenever the build/index.js file changes. Let’s leverage <a href="https://github.com/remy/nodemon/">nodemon</a> and <a href="https://github.com/1337programming/webpack-shell-plugin">webpack-shell-plugin</a>. The webpack-shell-plugin will allow us to run start the app using nodemon after webpack has finished building.</p><p>Install the packages above using yarn add --dev nodemon webpack-shell-plugin.</p><p>Add a run:dev script in package.json.</p><pre>...<br>&quot;scripts&quot;: {<br>  ...<br>  &quot;run:dev&quot;: &quot;NODE_ENV=development nodemon build/index.js&quot;<br>}</pre><p>Add the following in webpack.config.js.</p><pre>...<br>const WebpackShellPlugin = require(&#39;webpack-shell-plugin&#39;);</pre><pre>module.exports = {<br>  ...<br>  plugins: [<br>    new WebpackShellPlugin({<br>      onBuildEnd: [&#39;yarn run:dev&#39;]<br>    })<br>  ]<br>}</pre><p>I used the onBuildEnd option of the webpack-shell-plugin because it only runs the specified command(s) after the first build. On subsequent builds, nodemon will automatically restart the app.</p><p>Run yarn start:dev to run the app in development mode.</p><h3>Setting up unit testing</h3><p>For testing, we shall use <a href="https://jestjs.io/">jest</a> as our test runner and <a href="https://github.com/visionmedia/supertest">supertest</a> for testing the express app. Jest great support for typescript with a little configuration.</p><ol><li>Install jest, @types/jest, ts-jest and supertest as development dependencies using yarn add jest supertest @types/jest ts-jest --dev.</li><li>Add some configuration for jest. Create a jest.config.js file in the same directory as the package.json with the following content.</li></ol><pre>module.exports = {<br>  testEnvironment: &#39;node&#39;,<br>  transform: {<br>    &quot;^.+\\.ts$&quot;: &quot;ts-jest&quot;<br>  },<br>};</pre><p>The configuration above tells jest to transform typescript files to js before running the tests.</p><p>3. Create a tests folder on the same level as the src/ folder. This will contain tests and its contents will mirror the contents of src/.</p><p>4. Create a file with the name index.test.ts in the tests/ folder with the following contents. All test file names shall end in .test.ts.</p><pre>import app from &#39;../src/index&#39;;<br>import * as supertest from &#39;supertest&#39;;</pre><pre>describe(&#39;app&#39;, () =&gt; {<br>  let request;</pre><pre>  beforeEach(() =&gt; {<br>    request = supertest(app);<br>  });</pre><pre>  it(&#39;should return a successful response for GET /&#39;, done =&gt; {<br>    request.get(&#39;/&#39;)<br>      .expect(200, done);<br>  });<br>});</pre><p>5. Let’s modify src/index.ts to export the app variable as a default export and only start the server if the file is being executed as opposed to being imported.</p><pre>...</pre><pre>if (require.main === module) { // true if file is executed<br>  app.listen(PORT, () =&gt; {<br>    console.log(&#39;server started at <a href="http://localhost:&#39;+PORT);">http://localhost:&#39;+PORT);</a><br>  });<br>}</pre><pre>export default app;</pre><p>6. Add a “test” script in package.json.</p><pre>...<br>&quot;scripts&quot;: {<br>  ...<br>  &quot;test&quot;: &quot;jest&quot;<br>}</pre><p>7. Run yarn test to run the unit test above. This being jest you can pass some flags too such as yarn test --watch to watch the files, yarn test --coverage to let jest collect test coverage.</p><h4>Final thoughts.</h4><p>Running yarn start:dev yields a deprecation warning. This is because webpack-shell-plugin is using webpack’s old plugin API. You can follow up about this on this <a href="https://github.com/1337programming/webpack-shell-plugin/issues/61">issue</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=69d18c8c4f52" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-andela-way/how-to-set-up-an-express-api-using-webpack-and-typescript-69d18c8c4f52">How to set up an Express API using Webpack and Typescript.</a> was originally published in <a href="https://medium.com/the-andela-way">The Andela Way</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to create a masonry layout component using React.]]></title>
            <link>https://medium.com/the-andela-way/how-to-create-a-masonry-layout-component-using-react-f30ec9ca5e99?source=rss-6d678a7222de------2</link>
            <guid isPermaLink="false">https://medium.com/p/f30ec9ca5e99</guid>
            <category><![CDATA[intermediate]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[the-andela-way]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[John O. Paul]]></dc:creator>
            <pubDate>Fri, 05 Oct 2018 11:49:28 GMT</pubDate>
            <atom:updated>2018-10-05T11:49:28.680Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://masonry.desandro.com/layout.html">Masonry layout</a> is a kind of layout where the width or height of the elements are fixed while the other dimension is variable. It also ensures a uniform gap between elements.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*K-rYhVfxoFgWv7SXmQIKpQ.png" /><figcaption>Masonry layout</figcaption></figure><p>In this tutorial, we shall create a React component that renders its children using a uniform width masonry layout like in this <a href="https://codepen.io/john555/pen/GYoyNd">pen</a> I created. Below is how we expect the component to be used.</p><pre>&lt;MasonryLayout&gt;<br>  &lt;div&gt;item&lt;/div&gt;<br>  &lt;div&gt;item&lt;/div&gt;<br>  // more items<br>&lt;/MasonryLayout&gt;</pre><h4>Prerequisites.</h4><ul><li>Basic javascript knowledge such as declaring variables and working with arrays.</li><li>A functional React development environment. You can use <a href="https://github.com/facebook/create-react-app">create-react-app</a>.</li><li>Basic React — <a href="https://reactjs.org/docs/introducing-jsx.html">JSX</a>, functional components and propType declaration.</li><li>prop-types package. npm install --save prop-types</li></ul><h4>Creating the component.</h4><p>The component we’re going to create is going to be a functional component.</p><pre>import React from &#39;react&#39;;<br>import PropTypes from &#39;prop-types&#39;;</pre><pre>const MasonryLayout = props =&gt; {</pre><pre>}</pre><p>Our component will have some props — columns (number of columns in the masonry layout), gap(space between items) and children. We shall have to do some computations with column and gap, so their types will be number.</p><p>Let’s define the propTypes for the component and their default values as shown below.</p><pre>MasonryLayout.propTypes = {<br>  columns: PropTypes.number.isRequired,<br>  gap: PropTypes.number.isRequired,<br>  children: PropTypes.arrayOf(PropTypes.element),<br>};</pre><pre>MasonryLayout.defaultProps = {<br>  columns: 2,<br>  gap: 20,<br>};</pre><p>The children prop will be an array of elements that will be defined between the start (&lt;MasonryLayout&gt;)and end (&lt;/MasonryLayout&gt;) tag of our component. We are going to use this prop to access the items that our layout component will render.</p><p>We need to dynamically create arrays that will contain items in each column. Inside the MasonryLayout function, let’s create and declare some variables.</p><pre>const MasonryLayout = props =&gt; {<br>  const columnWrapper = {};<br>  const result = [];<br>}</pre><p>The columnWrapper object will enable us to dynamically create arrays that we can use as “buckets” for holding items in each column. The result array will contain column (jsx) elements. This is what shall be returned by the component.</p><p>Create the columns as shown below.</p><pre>// create columns<br>for (let i = 0; i &lt; props.columns; i++) {<br>  columnWrapper[`column${i}`] = [];<br>}</pre><p>Divide the children into columns as shown below.</p><pre>for (let i = 0; i &lt; props.children.length; i++) {<br>  const columnIndex = i % props.columns;<br>  columnWrapper[`column${columnIndex}`].push(<br>    &lt;div style={{ marginBottom: `${props.gap}px`}}&gt;<br>      {props.children[i]}<br>    &lt;/div&gt;<br>  );<br>}</pre><p>In the code above, were wrapping the child element with a div before pushing in the column. This enables us to add space between items in the column using bottom margin.</p><p>The next step will be wrapping the items in each column with a div and pushing it into the result array as shown below.</p><pre>for (let i = 0; i &lt; props.columns; i++) {<br>  result.push(<br>    &lt;div<br>      style={{<br>        marginLeft: `${i &gt; 0 ? props.gap : 0}px`,<br>        flex: 1,<br>      }}&gt;<br>      {columnWrapper[`column${i}`]}<br>    &lt;/div&gt;<br>  );<br>}</pre><p>We need space between the columns, so adding a left margin on all columns other than the first does the trick. The columns will be rendered in a flex container hence the flex: 1.</p><p>Let’s wrap the result in a flex container and return it.</p><pre>return (<br> &lt;div style={{ display: &#39;flex&#39; }}&gt;<br>   {result}<br>  &lt;/div&gt;<br>);</pre><h4>Using the component.</h4><p>Let’s render 12 div elements each with a random height between 200px and 500px.</p><pre>&lt;MasonryLayout columns={3} gap={25}&gt;<br>  {<br>    [...Array(12).keys()].map(key =&gt; {<br>      const height = 200 + Math.ceil(Math.random() * 300);<br>  <br>      return (<br>        &lt;div style={{height: `${height}px`}} /&gt;<br>       )<br>     })<br>  }<br>&lt;/MasonryLayout&gt;</pre><p>Congrats, we’re done creating a reusable masonry layout component. If you have any questions, engage me in the comments.</p><p><strong><em>Do you need to hire top developers? </em></strong><a href="https://hire.andela.com/need-more-devs"><strong><em>Talk to Andela to help you scale</em></strong></a><strong><em>. <br>Are you looking to accelerate your career as a developer? Andela is currently hiring senior developers. </em></strong><a href="http://bit.ly/2vpmyk8"><strong><em>Apply now</em></strong></a><strong><em>.</em></strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f30ec9ca5e99" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-andela-way/how-to-create-a-masonry-layout-component-using-react-f30ec9ca5e99">How to create a masonry layout component using React.</a> was originally published in <a href="https://medium.com/the-andela-way">The Andela Way</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to build a basic server side routing system in PHP.]]></title>
            <link>https://medium.com/the-andela-way/how-to-build-a-basic-server-side-routing-system-in-php-e52e613cf241?source=rss-6d678a7222de------2</link>
            <guid isPermaLink="false">https://medium.com/p/e52e613cf241</guid>
            <category><![CDATA[web]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[the-andela-way]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[intermediate]]></category>
            <dc:creator><![CDATA[John O. Paul]]></dc:creator>
            <pubDate>Tue, 03 Jul 2018 08:02:26 GMT</pubDate>
            <atom:updated>2018-07-24T10:45:19.034Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bvfc4WbLY5HV00VMCndazQ.jpeg" /><figcaption>Taken by <a href="https://medium.com/u/d10607adfa31"><strong><em>Aaron Sekisambu</em></strong></a></figcaption></figure><p>In the context of a server-side web application, a routing system is the part of the web application that maps an <a href="https://www3.ntu.edu.sg/home/ehchua/programming/webprogramming/HTTP_Basics.html">HTTP</a> request to a request handler (function/method). An HTTP request consists of a header and optionally a body. The header contains information about the request for example the method, path and host. Some methods like GET, HEAD, and OPTIONS do not make use of the request body while others like POST, PUT, and PATCH use it to pass data from a client to a server.</p><p>Using a routing system allows us to structure our application in a better way instead of designating each request to a file.</p><p>A routing system works by mapping an HTTP request to a request handler based on the request method and path specified in the URL of the request. This is exactly what we are going to build in this tutorial.</p><p><strong>Requirements.</strong></p><ul><li>You need PHP version 5 or higher running on your development machine.</li><li>Basic PHP knowledge including familiarity with PHP global variables.</li><li>An understanding of object oriented PHP.</li></ul><p><strong>Disclaimer.</strong></p><ul><li>This router will only handle GET and POST requests.</li><li>I am not going to use fancy tools and libraries. No composer, no autoloading. I want to keep focus on the concept.</li><li>Unit testing is great but I am not going to do it, for the purpose of keeping this tutorial short. (Sorry good developers)</li><li>The code I am going to write is not production ready code.</li></ul><h4>Let’s begin.</h4><p>Using the terminal, create a folder for the project and cd into the folder.</p><pre>$ mkdir php-router<br>$ cd php-router</pre><p>Create 4 files in the folder as shown below.</p><pre>$ touch index.php Request.php IRequest.php Router.php</pre><p><strong>index.php</strong> — This is the entry file for the web application. This is where we will initialize the router and define our routes. It shall also contain all imports.</p><p><strong>Request.php</strong> — This file will contain a class for the Request class for initializing objects that contain information about the HTTP request.</p><p><strong>IRequest.php</strong> —This file will contain the interface that the Request class must implement.</p><p><strong>Router.php </strong>— This file will contain the Router class.</p><p>Before writing the router, we need to design it’s API (How it will be used in apps). Below is the API for out router.</p><pre>$router = new Router;</pre><pre>// How GET requests will be defined</pre><pre>$router-&gt;get(&#39;/some/route&#39;, function($request) {<br>    // The $request argument of the callback <br>    // will contain information about the request<br>    return &quot;Content&quot;;<br>});</pre><pre>// How POST requests will be defined<br>$router-&gt;post(&#39;/some/route&#39;, function($request) {<br>    // How to get data from request body<br>    $body = $request-&gt;getBody();<br>});</pre><p>Now that we have defined how our Router will be used, let’s create it.</p><h4>Create the Request class.</h4><p>First, define the interface that the concrete Request class will implement. In IRequest.php define the IRequest interface.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f0ec4af24719d1e05137ce8f4dda6d4b/href">https://medium.com/media/f0ec4af24719d1e05137ce8f4dda6d4b/href</a></iframe><p><strong>getBody()</strong> retrieves data from the request body. The Request class must have the implementation for this method.</p><p>Create the Request class.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aa98d715231b945e5b1bba5d1edb6a02/href">https://medium.com/media/aa98d715231b945e5b1bba5d1edb6a02/href</a></iframe><p>In the constructor, invoke <strong>bootstrapSelf() </strong>— a method that sets all keys in the global <strong>$_SERVER</strong> array as properties of the Request class and assigns their values as well. This method makes a call to the <strong>toCamelCase()</strong> method.</p><p>The <strong>toCamelCase()</strong> method converts a string from <a href="https://en.wikipedia.org/wiki/Snake_case">snake case</a> to <a href="https://en.wikipedia.org/wiki/Camel_case">camel case</a>.</p><p><strong>getBody()</strong> is an implementation of the method defined in the <strong>IRequest</strong> interface.</p><h4>Create the Router class.</h4><p>In Router.php, add the following code for the Router.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/64f60a1b5b09e6599603ca91d7808b30/href">https://medium.com/media/64f60a1b5b09e6599603ca91d7808b30/href</a></iframe><p>In the Router class’ constructor, keep a reference to it’s dependency — the Request object.</p><p>Let’s use the PHP <a href="http://php.net/manual/en/language.oop5.magic.php">magic method</a> __call(). This method is triggered when invoking inaccessible methods in an object context.</p><p>Consider the code sample below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0eec01df6faf1b159125acafa9539b25/href">https://medium.com/media/0eec01df6faf1b159125acafa9539b25/href</a></iframe><p>We are trying to invoke the method <strong>get()</strong> which is not defined in class Router, so the <strong>__call() </strong>magic method is triggered. It receives two arguments. The first argument is the method name (<strong>‘get’</strong>) and the second is an array of arguments that the <strong>get()</strong> method was invoked with i.e [ ‘/’, function() {}].</p><p>We use the <strong>__call()</strong> magic method to dynamically create an associative array that map routes to callbacks. We create one for each supported request method. If an invalid method is called on the router object, we respond with a <strong>405 Method Not Allowed</strong>.</p><p>The <strong>resolve()</strong> method selects a callback that gets called to handle a request based on the request’s HTTP method and path (‘/’, ‘/foo/bar’, etc).</p><p>Now that we have our router, let’s use it to create an app.</p><h4>Create an app.</h4><p>In index.php, import the Request and Router classes we defined, initialize a router and define some routes.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a6c75f856f78eb2f6617cac5010f2fef/href">https://medium.com/media/a6c75f856f78eb2f6617cac5010f2fef/href</a></iframe><h4>Run and manually test the app.</h4><p>Let’s use PHP’s development server to run the app on port 8000.</p><p>While in the terminal, cd into the project folder and run the following command.</p><pre>$php -S 127.0.0.1:8000</pre><p>Open your web browser and navigate to <a href="http://localhost:8080/ipfs/QmSKzKDChVte8Bz7Kh13ZuAZ8ZKdhJRF7BRWGmVAt51uYW">http://localhost:8000/</a> and <a href="http://localhost:8000/profile">http://localhost:8000/profile</a> to test the home and profile pages.</p><p>Use <a href="https://www.getpostman.com/">Postman</a> or <a href="https://curl.haxx.se/">curl</a> (or any other tool of your choice) to test the POST route to <a href="http://localhost:8000/profile">http://localhost:8000/profile</a>.</p><p>Here is a screenshot of my test using postman.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*k18dhHNBOSL6CWYti75nLQ.png" /><figcaption>POST /data works</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kJliZ-lt2YkrgsTgPrsZ8Q.png" /><figcaption>DELETE /data returns 405 Method Not Allowed</figcaption></figure><h4>Final thoughts.</h4><p>PHP currently has poor support for HTTP methods that allow sending data in the request body, other than POST (i.e PUT, DELETE, PATCH, etc).</p><p>You have to use php://input (a read-only input <a href="http://php.net/manual/en/wrappers.php.php">stream</a>) to read raw data from the request body, then parse the data into an key-value pairs.</p><p><strong><em>Do you need to hire top developers? </em></strong><a href="https://hire.andela.com/need-more-devs"><strong><em>Talk to Andela to help you with that</em></strong></a><strong><em>.</em></strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e52e613cf241" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-andela-way/how-to-build-a-basic-server-side-routing-system-in-php-e52e613cf241">How to build a basic server side routing system in PHP.</a> was originally published in <a href="https://medium.com/the-andela-way">The Andela Way</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Create a pure CSS clock with SVG.]]></title>
            <link>https://medium.com/the-andela-way/create-a-pure-css-clock-with-svg-f123bcc41e46?source=rss-6d678a7222de------2</link>
            <guid isPermaLink="false">https://medium.com/p/f123bcc41e46</guid>
            <category><![CDATA[intermediate]]></category>
            <category><![CDATA[the-andela-way]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[webdev]]></category>
            <category><![CDATA[css]]></category>
            <dc:creator><![CDATA[John O. Paul]]></dc:creator>
            <pubDate>Fri, 27 Apr 2018 08:23:33 GMT</pubDate>
            <atom:updated>2018-04-27T08:23:33.660Z</atom:updated>
            <content:encoded><![CDATA[<p>Recently I decided to learn how to write SVG (Scalable Vector Graphics) by hand, as well as SVG animations. I have been using CSS for quite some time but I hadn’t used the steps <a href="https://www.w3schools.com/cssref/css3_pr_animation-timing-function.asp">animation timing function</a> before. It’s basically a function that transitions elements step by step, for the duration of the animation. <a href="https://codepen.io/john555/pen/LmZjmM">Here</a> is a pen I created that illustrates how the steps function works.</p><p>In this tutorial, we shall create this <a href="https://codepen.io/john555/pen/odxzVv">pure CSS clock</a>. This tutorial is divided into two sections:</p><ul><li>Section 1 — Creating a static clock.</li><li>Section 2 — Making the clock work.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/798/1*LYW5XnZ4XWIjACwj35HJmQ.png" /></figure><h4>Prerequisites</h4><ul><li>Basic SVG concepts like how to draw a <a href="https://www.w3schools.com/graphics/svg_line.asp">line</a> and a <a href="https://www.w3schools.com/graphics/svg_circle.asp">circle</a>.</li><li>CSS animations with <a href="https://www.w3schools.com/css/css3_animations.asp">keyframes</a>.</li><li><a href="https://www.w3schools.com/cssref/css3_pr_transform.asp">CSS transforms</a>.</li></ul><h3><strong>Section 1 — Creating a static clock.</strong></h3><p>First, Let’s create an outline of the shape of the clock. It’s a simple circle with a white fill and a black stroke.</p><p><strong>HTML</strong></p><p>The viewBox property of the SVG specifies the x, y, width and height properties respectfully. The radius of the circle is set to19 so that it can fit into the viewBox.</p><pre>&lt;svg viewBox=&quot;0 0 40 40&quot;&gt;<br>  &lt;circle cx=&quot;20&quot; cy=&quot;20&quot; r=&quot;19&quot; /&gt;<br>&lt;/svg&gt;</pre><p><strong>CSS</strong></p><p>Give the background a light gray color and style the SVG element.</p><pre>html {<br>  background: #dedede;<br>}</pre><pre>svg {<br>  width: 400px;<br>  fill: white;<br>  stroke: black;<br>  stroke-width: 1;<br>  stroke-linecap: round;<br>}</pre><p>Add 12 marks to indicate the hours on a clock.</p><p><strong>HTML</strong></p><p>The &lt;g&gt; tag groups the line elements so that, later we can move them at once using <a href="https://www.w3schools.com/cssref/css3_pr_transform.asp">CSS transforms</a>. Note that the lines are 12 in total (one for each hour) and have the same coordinates. The &lt;g&gt; element is by default positioned at (0, 0) — the top-left of the viewBox. This is true for all SVG elements.</p><p>The &lt;line&gt; elements have similar values of x1, y1, x2 and y2 so that they are of the same height.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6cc95d2446f085434788c1c9d1565fd2/href">https://medium.com/media/6cc95d2446f085434788c1c9d1565fd2/href</a></iframe><p><strong>CSS</strong></p><p>Move &lt;g&gt; element to the center and rotate the lines in intervals of 30 degrees to make them equally spaced around the clock. Let’s also make the 3, 6, 9 and 12 hour marks thicker than the rest by setting theirstroke-width to 0.5 and for the rest of the hands to 0.2.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9a0d63661b91c401ef4ec926127f5bd0/href">https://medium.com/media/9a0d63661b91c401ef4ec926127f5bd0/href</a></iframe><p>Let’s add the moving hands and a pin that holds them in place.</p><p><strong>HTML</strong></p><p>The order of these elements matters. It affects their z-index. Elements that come later in the DOM appear above those that come before them.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b46c8ff5772137d44cb4631ff01fc9a4/href">https://medium.com/media/b46c8ff5772137d44cb4631ff01fc9a4/href</a></iframe><p><strong>CSS</strong></p><p>Move the hands of the clock to the center and give them different values of stroke-width to distinguish them.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e168ea619fff665bf9eceabe6a809bd0/href">https://medium.com/media/e168ea619fff665bf9eceabe6a809bd0/href</a></iframe><p>This is the output of the code we have written so far.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/806/1*ak4MKA-Cz8_xiCD40oExRw.png" /></figure><p>From the image above, you can see that the hands are pointing at the 3 O’clock mark (at a rotate(0deg)). Let’s move the hands to the 12 O’clock mark by rotating the entire SVG by -90deg (-ve degrees rotate anti-clockwise).</p><p><strong>CSS</strong></p><p>Add the following CSS properties to the svg element to make the hands point to the 12 O’clock mark.</p><pre>svg {<br>  ...<br>  transform: rotate(-90deg);<br>}</pre><p>Add some text (for branding purposes) that says, “#TIA” — <strong>This is Andela</strong>.</p><p><strong>HTML</strong></p><p>Add the following mark up <strong>before</strong> the line elements that draw the hands to make it appear below the hands.</p><pre>&lt;text x=&quot;0&quot; y=&quot;0&quot; class=&quot;tiaText&quot;&gt;#TIA&lt;/text&gt;</pre><p><strong>CSS</strong></p><p>Position and style the text above.</p><pre>.tiaText {<br>  font-size: 1px;<br>  font-family: sans-serif;<br>  transform: translate(14px, 19px) rotate(90deg);<br>  fill: #dfdfdf;<br>  stroke: none;<br>}</pre><p>This is what your clock should look like if everything is done right. :)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/794/1*9t1eiieT5PIxe7lTSSccZg.png" /></figure><p>What’s a clock that doesn’t tick? — A dead clock. Let’s make ours work.</p><h3>Section 2 — Making the clock work.</h3><p>In order for us to pull off this job, we’re going to employ <a href="https://www.w3schools.com/css/css3_variables.asp">CSS variables</a>.</p><p>Add some CSS variables to reference the time when the page loads. Add the following CSS variables to the svg element’s selector.</p><pre>svg {<br>  ...<br>  --start-seconds: 57;<br>  --start-minutes: 45;<br>  --start-hours: 11;<br>}</pre><p>Make the hands correctly indicate the time set above.</p><p><strong>CSS</strong></p><p>Let’s use CSS transforms to position the hands to the center and rotate them to indicate the appropriate seconds, minutes, or hours set above.</p><p><strong><em>Formula:</em></strong><em> (</em>Number of seconds/minutes) * 6degrees = rotation of the respective hand for both the seconds and minutes hands.</p><p>The clock can only indicate 12 hours so we multiply the number of hours by 30degrees to compute the rotation of the hour hand.</p><p><strong>CSS</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5896be5a1215dd725297b2ad80132d87/href">https://medium.com/media/5896be5a1215dd725297b2ad80132d87/href</a></iframe><p>To make the<strong> seconds hand </strong>work, we shall define an animation called rotateSecondsHand and use it on the seconds hand (.seconds). The animation timing function is steps(60).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/168a77166dc0c241316c388e77808f5b/href">https://medium.com/media/168a77166dc0c241316c388e77808f5b/href</a></iframe><p>To make the<strong> minute hand </strong>work, we shall define an animation called rotateMinuteHand and use it on the minute hand (.minute).</p><p><strong>CSS</strong></p><p>The animation timing function is steps(60). We need to set the animation-delay to a negative value based on the value of --start-seconds. We do this to “forward” the animation, so that the minute hand moves whenever the seconds hand reaches the 12 O’clock mark.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8257caa958ec820f98d624421ad5134a/href">https://medium.com/media/8257caa958ec820f98d624421ad5134a/href</a></iframe><p>Finally, let’s make the <strong>hour hand</strong> work. Note that for the hour hand (.hour), animation-delay is dependent on --start-seconds and --start-minutes.</p><p><strong>CSS</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f9289323ef1cc67e44c1319c78b5206b/href">https://medium.com/media/f9289323ef1cc67e44c1319c78b5206b/href</a></iframe><p>Here the animation delay is dependent on the values of --start--seconds &amp; --start-minutes.</p><p><strong>How to test the clock.</strong></p><ul><li>To test the minute hand, set--start--seconds:59, --start-minutes:59 and --start-hours:11. The minute hand should move one step with the seconds hand and stop. After that, the minute hand should move whenever the seconds hand reaches the 12 hour mark.</li><li>To test the hour hand, set--start--seconds:30, --start-minutes:30 and --start-hours:11 . The hour hand should be midway between the 11 and 12 O’clock marks.</li></ul><p><strong>Final thoughts</strong></p><p>Until CSS can enable us to tell time, we need JavaScript to set the correct time. The accuracy of this clock will depend on that of your device’s time. Here is the JavaScript code that does the job.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3a4b6625154cda3c485e9ec6f1a6e142/href">https://medium.com/media/3a4b6625154cda3c485e9ec6f1a6e142/href</a></iframe><p>This is still a pure CSS clock because it’s functionality is completely in written CSS.</p><p><em>Thanks for making it to the end of the tutorial. If you feel I have left out some explanation(s), feel free to engage me in a discussion via the comments.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f123bcc41e46" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-andela-way/create-a-pure-css-clock-with-svg-f123bcc41e46">Create a pure CSS clock with SVG.</a> was originally published in <a href="https://medium.com/the-andela-way">The Andela Way</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>