<?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 MESHILEYA Emmanuel Seun on Medium]]></title>
        <description><![CDATA[Stories by MESHILEYA Emmanuel Seun on Medium]]></description>
        <link>https://medium.com/@meshileya?source=rss-fd526b7a7666------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*4rKvE8JF6L4Z7W24Ok0LZA.jpeg</url>
            <title>Stories by MESHILEYA Emmanuel Seun on Medium</title>
            <link>https://medium.com/@meshileya?source=rss-fd526b7a7666------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 00:17:41 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@meshileya/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[Simplified Angular unit testing]]></title>
            <link>https://medium.com/@meshileya/simplified-angular-unit-testing-caffe0fccf31?source=rss-fd526b7a7666------2</link>
            <guid isPermaLink="false">https://medium.com/p/caffe0fccf31</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[unittest]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[angularjs]]></category>
            <dc:creator><![CDATA[MESHILEYA Emmanuel Seun]]></dc:creator>
            <pubDate>Mon, 08 Oct 2018 09:56:42 GMT</pubDate>
            <atom:updated>2018-10-08T09:56:42.588Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*Ma_qz3Xs7PZvjjT-fPUaOw.png" /></figure><h3>Introduction</h3><p>This article focuses on how to write (simple) unit test for your Angular modeling blocks (service, component, async task etc). We will be using a simple Quotes application to demonstrate how you can write a unit test for your project. I assume you already have an understanding of how to use Angular 2+. If you are a beginner or you have interest in Angular, you can find more <a href="https://angular.io/docs">here</a>.</p><h4>Why do you need to test your application?</h4><p>Have you been looking for a way to test what you have built based on user behavior? I really don’t expect you to test each behavior one at a time. That does not only waste your time but it is also ineffective. Writing tests for different coupling blocks in your application will help demonstrate how these blocks behave. The Quotes application we will be looking at has a service, a component and an async task to simulate data being fetched from the server.</p><p>One of the easiest ways to test the strengths of these blocks is writing a test for each of them. You don’t necessarily need to wait until your users complain how the input field behaves when the button is clicked. Writing a test for your blocks (components, services etc) can easily detect when there is a break.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0G8sf2PiH5v3gUJEjzHjjw.gif" /><figcaption>A simple Quote application</figcaption></figure><h4>How do you set up an Angular test ?</h4><p>When you create a new project with the cli (ng new appName), a default component and test file are added. Also, for those that always like a shortcut method like me, a test script is always created alongside any component module (service, component) you create using angular cli(Command Line Interface).</p><p>This test script which ends with .spec.ts is always added. Let’s take a look at the initial test script file which is the app.component.spec.ts :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9168599325d51a2ddbedc91675576331/href">https://medium.com/media/9168599325d51a2ddbedc91675576331/href</a></iframe><p>Let’s run our first test to make sure nothing has broken yet:</p><pre>ng test</pre><p>You might be wondering, how can we simulate a user behavior by simply writing a test, even though the project is being rendered in a browser? As we proceed, I will demonstrate how we can simulate the app running on a browser.</p><p>By default, Angular runs on Karma which is a test runner that runs the unit tests snippet like the above app.component.spec.ts file. Karma also ensures the result of the test is printed out either in the console or in file log. Other test runners are mocha, jasmine etc.</p><h4>How does the test run?</h4><p>The testing package has some utilities (TestBed, async). TestBed is the main Angular utility package.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/461/1*VsV6MMgka1zlPGYdgLwjuQ.jpeg" /><figcaption>flow of running test</figcaption></figure><p>The describe container contains different blocks (it, beforeEach, xitetc). The beforeEach runs before any other block while others do not depend on each other to run.</p><p>From the app.component.spec.ts file, the first block is the beforeEach inside the container (describe). This is the only block that runs before any other block (it). The declaration of the app module in app.module.ts file is simulated (declared) in the beforeEach block. The component (AppComponent) declared in the beforeEach block is the main component we want to have in this testing environment. The same logic applies to other test declaration.</p><p>The compileComponents object is called to compile your component’s resources like the template, styles etc. You might not necessarily compile your component if you are using webpack:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f9e99a1e79b034f3b66b87ae04e2259/href">https://medium.com/media/1f9e99a1e79b034f3b66b87ae04e2259/href</a></iframe><p>Now that the component has been declared in the beforeEach block, let’s check if the component is created.</p><p>Thefixture.debugElement.componentInstance creates an instance of the class (AppComponent) . We will test to see if the instance of the class is truly created or not using toBeTruthy :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ed950c9e50430fb0084edec896780727/href">https://medium.com/media/ed950c9e50430fb0084edec896780727/href</a></iframe><p>The third block demonstrates how you can have access to the properties of the created component(AppComponent). The only property added by default is the title. You can easily check if the title you set has changed or not from the instance of the component (AppComponent) created:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fc3732380339f92f227b4ca0e0f7a16e/href">https://medium.com/media/fc3732380339f92f227b4ca0e0f7a16e/href</a></iframe><p>The fourth block demonstrates how the test behaves in the browser environment. After creating the component, an instance of the created component (detectChanges) to simulate running on the browser environment is called. Now that the component has been rendered, you can have access to its child element by accessing the nativeElelment object of the rendered component (fixture.debugElement.nativeElement):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bfad54da0da5e8294b59ba54ba5d94d0/href">https://medium.com/media/bfad54da0da5e8294b59ba54ba5d94d0/href</a></iframe><p>Now that you have familiarized yourself with the basics of testing a component. Let’s test theQuote application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/299/1*c0OkVXCddvparDMFW6FkYg.jpeg" /><figcaption>Overview of Quote application</figcaption></figure><h4>How to test a service(QuoteService)</h4><p>Services often depend on other services that Angular injects into the constructor. In many cases, it easy to create and <em>inject</em> these dependencies by adding providedIn: root to the injectable object which makes it accessible by any component or service:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f8473b5e1781241353f92bc2f6d15657/href">https://medium.com/media/f8473b5e1781241353f92bc2f6d15657/href</a></iframe><p>Here are a few ways to test the QuoteService class:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d5716504cc208b1491a789073029cba8/href">https://medium.com/media/d5716504cc208b1491a789073029cba8/href</a></iframe><p>In the first block, beforeEach, an instance of QuoteService is created to ensure its only created once and to avoid repetition in other blocks except for some exceptional cases:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/566c230f8c35d6aacab16f11216ab760/href">https://medium.com/media/566c230f8c35d6aacab16f11216ab760/href</a></iframe><p>The first block tests if the post model QuoteModel(text, date) is created into an array by checking the length of the array. The length of the quoteList is expected to be 1:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/60b7caf95872c01f81509771f8e60098/href">https://medium.com/media/60b7caf95872c01f81509771f8e60098/href</a></iframe><p>The second block creates a post in an array and removes it immediately by calling removeQuote in the service object. The length of the quoteList is expected to be 0.</p><h4>How to test a component (QuotesComponent)</h4><p>The service is injected into the QuoteComponent in order to have access to its properties which will be needed by the view:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9eb0e047dca2952773a8431a4acb3887/href">https://medium.com/media/9eb0e047dca2952773a8431a4acb3887/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0b2f883689c0a0a907c03afa14580d49/href">https://medium.com/media/0b2f883689c0a0a907c03afa14580d49/href</a></iframe><p>The first two blocks in the describe container run consecutively. In the first block, the FormsModule is imported into the configure test. This ensures the forms related directives like ngModel can be used.</p><p>Also, the QuotesComponent is declared in the configTestMod just like the way components are declared in ngModule residing in the appModule file. The second block creates a QuoteComponent and its instance which would be used by the other blocks:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/698662e973b5360ea093fb3447cdf628/href">https://medium.com/media/698662e973b5360ea093fb3447cdf628/href</a></iframe><p>This block tests if the instance of the component that is created is defined:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/10f3ab061b3e91962a3ba4978f6cbbe2/href">https://medium.com/media/10f3ab061b3e91962a3ba4978f6cbbe2/href</a></iframe><p>The injected service handles the manipulation of all operations (add, remove, fetch). The quoteService variable holds the injected service (QuoteService). At this point, the component is yet to be rendered until the detectChanges method is called:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7d3123ea9934bb77eb6ae7c222da4971/href">https://medium.com/media/7d3123ea9934bb77eb6ae7c222da4971/href</a></iframe><p>Now let’s test if we can successfully create a post. The properties of the component can be accessed upon instantiation, so the component rendered detects the new changes when a value is passed into the quoteText model. The nativeElement object gives access to the HTML element rendered which makes it easier to check if the quote added is part of the texts rendered:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/145afdc7fac2ae099a133f9c71172461/href">https://medium.com/media/145afdc7fac2ae099a133f9c71172461/href</a></iframe><p>Apart from having access to the HTML contents, you can also get an element by its CSS property. When the quoteText model is empty or null, the button is expected to be disabled:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/812eadb116dd9f6a358e492984292df5/href">https://medium.com/media/812eadb116dd9f6a358e492984292df5/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6629b4c77de3b5b9c270721dfdbe71f1/href">https://medium.com/media/6629b4c77de3b5b9c270721dfdbe71f1/href</a></iframe><p>Just like the way we access an element with its CSS property, we can also access an element by its class name. Multiple classes can be accessed at the same time using By e.g By.css(‘.className.className’) . The button clicks are simulated by calling the triggerEventHandler . The event type must be specified which is click in this case. A quote displayed is expected to be deleted from the quoteList when clicked on:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/254dd2f24c5b4d5b5616718641f57d89/href">https://medium.com/media/254dd2f24c5b4d5b5616718641f57d89/href</a></iframe><h4>How do you test an asynchronous operation?</h4><p>You can’t escape a time you will need to fetch data remotely. This operation is best treated as an asynchronous task.</p><p>fetchQoutesFromServer represents an async task which returns an array of quotes after two seconds:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ea9a5e77a2360a7a7d700a5b20fae12a/href">https://medium.com/media/ea9a5e77a2360a7a7d700a5b20fae12a/href</a></iframe><p>spyOn objects simulate how fetchQuotesFromServer method works. It accepts two argument quoteService which is injected into the component and the method fetchQuotesFromServer . fetchQuotesFromServer is expected to return a promise. spyOn chains the method using and with a fake promise call which is returned using returnValue . Since we want to fake how the fetchQuotesFromServer works, we need to pass a promise that will resolve with a list of quotes.</p><p>Just as we have done before, the detectChanges method is called to get the updated changes. whenStable allows access to results of all async tasks when they are done:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/554cb99ba791f227a6e1f41d9d85881c/href">https://medium.com/media/554cb99ba791f227a6e1f41d9d85881c/href</a></iframe><h4>Conclusion</h4><p>Angular also ensures test results are viewed in your browser. This will give a better visualization of the test results.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZSUAwwNh3CoIkDYSntC84A.png" /><figcaption>Test Result</figcaption></figure><p>The source code to the project can be found <a href="https://github.com/meshboy/angular-unit-test">here</a>.</p><p>NB: Originally published on <a href="https://logrocket-blog.ghost.io/angular-unit-testing">LogRocket</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=caffe0fccf31" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A forum engine using Nodejs and Vuejs (part 3)]]></title>
            <link>https://medium.com/@meshileya/a-forum-engine-using-nodejs-and-vuejs-part-3-676504f33c23?source=rss-fd526b7a7666------2</link>
            <guid isPermaLink="false">https://medium.com/p/676504f33c23</guid>
            <category><![CDATA[api]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[MESHILEYA Emmanuel Seun]]></dc:creator>
            <pubDate>Sat, 19 May 2018 08:38:21 GMT</pubDate>
            <atom:updated>2018-05-19T16:37:36.083Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*-Nd4WoGNdHt6O0Xm-cYlMA.jpeg" /></figure><p>It almost took a “<em>whole decade” </em>to wrap up the last section. It seems I need a bit of catching up to do with writing and working.</p><p>Without wasting much time, lets dive in. Though this is the last section of the forum-engine-api. It really took a while to come up with this last part though. The full source code can be found <a href="https://github.com/meshboy/forum-engine-api">here</a> while the previous post can be found <a href="https://medium.com/@meshileya/a-forum-engine-using-nodejs-and-vuejs-part-2-8c385aa2fea6">here</a>. For proper understanding, each section as described in <a href="https://medium.com/@meshileya/a-forum-engine-using-nodejs-and-vuejs-10b5939ebf2e">part one</a> are divided into different git branches. It would be nice, if the project is downloaded locally to aid better understanding as we move forward.</p><p>Lets create a test case to ensure the controllers work as expected. For every Restful service (api), four verbs are widely used. <br><strong>GET</strong> to fetch data, <strong>POST </strong>(create new data), <strong>PUT</strong> (update created data) , <strong>DELETE</strong> (remove created data).</p><p><em>/src/api/resources/post/post.test.js</em></p><pre>describe(&#39;query&#39;, () =&gt; {<br>  describe(&#39;createOne&#39;, () =&gt; {<br>    it(&#39;should create one document&#39;, <strong>async </strong>() =&gt; {<br>      <strong>const </strong>body = {<br>        title: &#39;test post&#39;,<br>        user: user._id,<br>      };<br>      <strong>const </strong>result = <strong>await </strong>controllers.createOne(Post, body);<br>      expect(result).to.be.ok;<br>      expect(result._id).to.be.ok;<br>      expect(result.title).to.equal(&#39;test post&#39;);<br>    });<br>  });<br>});</pre><p>Remember, we are only dealing with two models here (User and Post) based on the type of project we are working on. So, we need a controller that would be able to create, delete, get single or all and update resource. The above test case ensures, either of the models used would be able to create data. Post model is used in this case.</p><p><strong><em>A quick look in the controller</em></strong></p><p>Creating a new document would require new input data from the user. The data is held in the req (request). E.g username = req.body.username. <br>To create a document in mongoose, one of the options is to call the <strong>create</strong> method on the appropriate Model. E.g Post.create(……) which returns a promise. <br>The snippet below accepts a model to create a document while the body is the document to be created.</p><p><em>/src/api/modules/query.js</em></p><pre><strong>export const </strong>controllers = {<br>  createOne(model, body) {<br>    <strong>return </strong>model.create(body);<br>  }<br>};</pre><p>The promise returned is being resolved while the response is being sent back to the client. According to norms, its ideal to send the document created back to the client. The <strong>next</strong> param ensures <strong>flows </strong>needed to be executed <em>serially</em> is achieved. E.g. <em>check if a user is an admin before fetching all users</em>. In this project, the error(s) is/are passed to the next guy while the next guy passes to the next guy until it sees a function that resolves it (error).</p><pre><strong>export const </strong>createOne = model =&gt; (req, res, next) =&gt; controllers.createOne(model, req.body)<br>    .then(doc =&gt; res.status(201).json(doc))<br>    .catch(error =&gt; next(error));</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/685/1*hELTb-lAW6OXraVotRwzNA.png" /><figcaption>how <strong>next</strong> works</figcaption></figure><pre>findByParam(model, id) {<br>  <strong>return </strong>model.findById(id);<br>},</pre><p>At some point in time, we would need to get a single document, The most handy way is to get the document by its id. The snippet below get a document and moves to the <strong>next</strong> available call if there is one else just throw an error and pass to where it would be resolved. As we proceed we would see the importance of this method.</p><pre><strong>export const </strong>findByParam = model =&gt; (req, res, next, id) =&gt; controllers.findByParam(model, id)<br>    .then(doc =&gt; {<br>      <strong>if </strong>(!doc) {<br>        next(<strong>new </strong>Error(&#39;Not Found Error&#39;));<br>      } <strong>else </strong>{<br>        req.docFromId = doc;<br>        next();<br>      }<br>    })<br>    .catch(error =&gt; {<br>      next(error);<br>    });</pre><p>Finally, the controller is ready. There are two important parameters.</p><p><strong><em>model (Post, User) : </em></strong><em>Once the model is passed. It inherits the available methods. well, there are some points the available methods would not be sufficient. We can always come up with our custom method(s) and pass it as an override.</em></p><pre><strong>export const </strong>generateControllers = (model, overrides = {}) =&gt; {<br>  <strong>const </strong>defaults = {<br>    findByParam: findByParam(model),<br>    getAll: getAll(model),<br>    getOne: getOne(model),<br>    deleteOne: deleteOne(model),<br>    updateOne: updateOne(model),<br>    createOne: createOne(model),<br>  };<br><br>  <strong>return </strong>{ ...defaults, ...overrides };<br>};</pre><p>/src/api/resources/post/post.controller.js</p><p>Here we go, we need to populate(get the original document) the <strong>user </strong>object also in the comment object. So we need a custom <strong>getAll </strong>also a body object validation was demonstrated in <em>updateOne</em> method using <a href="https://github.com/hapijs/joi"><strong>joi</strong></a></p><pre><strong>export default </strong>generateControllers(Post, { getAll, updateOne });</pre><p><em>get all posts</em> (populate the owner of the post alongside the users who have commented on the post, ensure only the email is returned to avoid the users password and any other relevant information be sent back to the client).</p><pre><strong>const </strong>getAll = (req, res, next) =&gt; {<br>  Post.find({}).populate(&#39;user comment.user&#39;, { email: 1 }).exec()<br>    .then(docs =&gt; res.status(200).json({ status: <strong>true</strong>, data: docs }))<br>    .catch(error =&gt; next(error));<br>};</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/443/1*jWJmmVUoA8Q5KUT2kp8lhQ.png" /></figure><p><em>update post</em></p><p><strong>{ new: true }</strong> ensures the last updated document is returned.</p><pre><strong>const </strong>updateOne = (req, res, next) =&gt; {<br>  <strong>const </strong>message = req.body.message;<br>  <strong>const </strong>doc = req.docFromId;<br>  <strong>const </strong>user = req.body.user;<br><br>  <strong>const </strong>schema = Joi.object().keys({<br>    message: Joi.string().required(),<br>  });<br><br>  <strong>const </strong>result = Joi.validate({ message }, schema);<br><br>  <strong>if </strong>(result.error) {<br>    res.status(400).json({ status: <strong>false</strong>, message: result.error });<br>  } <strong>else </strong>{<br>    <strong>const </strong>commentObject = {<br>      user,<br>      message,<br>    };<br><br>    Post.findByIdAndUpdate(doc._id, { $push: { comment: commentObject } }, { <strong>new</strong>: <strong>true </strong>})<br>      .exec()<br>      .then(doc =&gt; {<br>        res.status(200).json({ status: <strong>true</strong>, data: doc });<br>      })<br>      .catch(error =&gt; next(error));<br>  }<br>};</pre><p>express.Router() provides an avenue for mini app as compared to using the variable express ( <strong>const app = express(); </strong>). It ensures the project is broken into modular apps.</p><p><strong><em>postRouter.param</em></strong><em> is called immediately the client fires a request that contains id in the url </em><strong><em>paths</em></strong><em> (e.g post/:id).</em></p><pre><strong>import </strong>express <strong>from </strong>&#39;express&#39;;<br><strong>import </strong>PostController <strong>from </strong>&#39;./post.controller&#39;;</pre><pre><strong>export const </strong>postRouter = express.Router();<br><br>postRouter.param(&#39;id&#39;, PostController.findByParam);</pre><pre>//the same route can be used for more than one VERB if the route url //is the same<br>postRouter.route(&#39;/&#39;)<br>  .get(PostController.getAll)<br>  .post(PostController.createOne);<br><br>postRouter.route(&#39;/:id&#39;)<br>  .get(PostController.getOne)<br>  .put(PostController.updateOne)<br>  .delete(PostController.deleteOne);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/509/1*56wOeC6FNujxf551tVvkoQ.png" /><figcaption>Post is updated</figcaption></figure><p>The post router and the error handler are mounted on the router which makes it easy to be used wherever its being called. Check here to get more <a href="https://expressjs.com/en/guide/routing.html"><strong>info</strong></a><strong>.</strong></p><pre><strong>export const </strong>router = express.Router();<br>router.use(&#39;/post&#39;, postRouter);<br>router.use(errorHandler);</pre><p><em>errorHandler (check out the parameters). The error parameter tells express, hey man, am the guy to handle whatever error you are bringing down. Its best to ensure the error handler is mounted last. <br></em><strong><em>mountA(), mountB(), mountC(), errorHandler()</em></strong><em><br>if the errorHandler is placed before mountC() and mountC() contains an error. The app crashes because express keeps looking for the handler (function) to handle the error. Once it does not see it… Boom!!! (the app crashes and huge money would be lost).</em></p><p><em>/src/api/modules/errorHandler.js</em></p><pre><strong>export const </strong>errorHandler = (error, req, res, next) =&gt; {<br>  console.error(error.stack);<br>  res.status(500).json({<br>    status: <strong>false</strong>,<br>    message: error.toString() || &#39;something went wrong. Please try again&#39;,<br>  });<br>};</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/616/1*ZwvQTC55q6Z3J7NU5gBTiQ.png" /><figcaption>error handler resolved validation error</figcaption></figure><p>/src/api/modules/errorHandler.js</p><p>Using the jwt (json web token), it wraps up data and returns tokenised value for the data. The function below wraps up id (user id) and returns a tokenised value which expires at a given time;</p><pre><strong>export const </strong>signIn = id =&gt; Jwt.sign({ id }, secret, { expiresIn });</pre><p>checkToken is inherited from express jwt. Its the security guard that mornitors each request, it checks if a valid token is passed appropriately. A valid token is passed in this format <em>Bearer ${req.headers.access_token} . Once the token is valid, the next param is called, the request goes through if not, the request throws and error, and the error handler comes into play again.</em></p><pre><strong>const </strong>validateToken = (req, res, next) =&gt; {<br>  checkToken(req, res, next);<br>};</pre><pre><strong>export const </strong>decodeToken = () =&gt; (req, res, next) =&gt; {<br>  <strong>if </strong>(req.headers.access_token) {<br>    req.headers.authorization = `Bearer ${req.headers.access_token}`;<br>  }<br>  validateToken(req, res, next);<br>};</pre><p>for every successful authentication, a token is generated</p><pre><strong>export const </strong>loginUser = (req, res, next) =&gt; {<br>  <strong>const </strong>email = req.body.email;<br>  <strong>const </strong>password = req.body.password;<br><br>  // validate the data coming from the client before processing<br>  <strong>const </strong>schema = Joi.object().keys({<br>    email: Joi.string().email(),<br>    password: Joi.string().required(),<br>  });<br><br>  <strong>const </strong>result = Joi.validate({ email, password }, schema);<br><br>  <strong>if </strong>(result.error) {<br>    res.status(400).json({ status: <strong>false</strong>, message: result.error });<br>  } <strong>else </strong>{<br>    User.findOne({ email }).exec()<br>      .then(user =&gt; {<br>        <strong>if </strong>(user) {<br>          <strong>if </strong>(user.comparePassword(password)) {<br>            req.user = user;<br>            res.status(200)<br>              .json({ status: <strong>true</strong>, data: { token: signIn(req.user._id) } });<br>          } <strong>else </strong>{<br>            res.status(401)<br>              .json({ status: <strong>false</strong>, message: &#39;Password is incorrect&#39; });<br>          }<br>        } <strong>else </strong>{<br>          res.status(404)<br>            .json({ status: <strong>false</strong>, message: &#39;User does not exist! Kindly register&#39; });<br>        }<br>      })<br>      .catch(error =&gt; next(error));<br>  }<br>};</pre><p>as long as the token is valid and decoded, we can easily retrieve back the <strong><em>id</em></strong>, which was stored in <strong><em>req.user</em></strong> by <a href="https://github.com/auth0/express-jwt">expressjwt</a>. The user can easily be retrieved by its id and stored in <strong><em>req</em></strong> for easy reuse by any controller.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/643/1*R_Qtn-QWt6FxaA_b4N-OAw.png" /><figcaption>login</figcaption></figure><pre><strong>export const </strong>getUser = () =&gt; (req, res, next) =&gt; {<br>  User.findById({ _id: req.user.id })<br>    .then(user =&gt; {<br>      <strong>if </strong>(user) {<br>        req.user = user;<br>        // get user id and use where necessary<br>        req.body.user = user.id;<br>        next();<br>      } <strong>else </strong>{<br>        res.json({ status: <strong>false</strong>, message: &#39;User not found&#39; });<br>      }<br>    })<br>    .catch(error =&gt; next(error));<br>};</pre><p>same logic for generating a fresh token when a user login is also applied in creating a new user.</p><pre><strong>export const </strong>createUser = (req, res, next) =&gt; {<br>  <strong>const </strong>email = req.body.email;<br>  <strong>const </strong>password = req.body.password;<br><br>  // validate the data coming from the client before processing<br>  <strong>const </strong>schema = Joi.object().keys({<br>    email: Joi.string().email(),<br>    password: Joi.string().required(),<br>  });<br><br>  <strong>const </strong>result = Joi.validate({ email, password }, schema);<br><br>  <strong>if </strong>(result.error) {<br>    res.status(400).json({ status: <strong>false</strong>, message: result.error });<br>  } <strong>else </strong>{<br>    User.findOne({ email })<br>      .then(user =&gt; {<br>        <strong>if </strong>(user) {<br>          res.status(400).json({ status: <strong>false</strong>, message: &#39;email already exist&#39; });<br>        } <strong>else </strong>{<br>          <strong>const </strong>newUser = <strong>new </strong>User();<br>          newUser.email = email;<br>          newUser.passwordHash = newUser.generateHashPassword(password);<br><br>          newUser.save().then(doc =&gt; {<br>            req.user = doc;<br>            res.status(200)<br>              .json({ status: <strong>true</strong>, data: { token: signIn(req.user._id) } });<br>          }).catch(error =&gt; next(error));<br>        }<br>      })<br>      .catch(error =&gt; next(error));<br>  }<br>};</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/852/0*gJ9W0RpFduBXGJCP.jpg" /></figure><p><strong><em>secure</em></strong><em> would be injected into all routes we would like to secure or else, our data would be accessed freely without restrictions.</em></p><p>The functions in the array run serially. <strong>decodeToken</strong> is called before <strong>getUser </strong>before the appropriate route is finally allowed to proceed. if the decodeToken is not called first, there would not be an id <strong>unwrapped</strong> from the token to get the user from <strong>getUser()</strong></p><pre><strong>export const </strong>secure = [decodeToken(), getUser()];</pre><p><em>/src/server.js</em></p><p>each piece is mounted in the main app (express) and secured where necessary</p><pre><strong>const </strong>app = express();<br><br>middleware(app);<br>connect();<br><br>app.use(&#39;/login&#39;, loginUser);<br>app.use(&#39;/register&#39;, createUser);<br>app.use(&#39;/api/v1/&#39;, secure, router);<br>app.use(errorHandler);<br><strong>export default </strong>app;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/575/1*bNC4uSoVI-ei38ihqUrxwg.png" /><figcaption>trying to access a secured route without a token</figcaption></figure><p>And this brings us to the end of the server side …. The next post would be centered on how the client communicates with the api and vice versa.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=676504f33c23" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A forum engine using Nodejs and Vuejs (part 2)]]></title>
            <link>https://medium.com/@meshileya/a-forum-engine-using-nodejs-and-vuejs-part-2-8c385aa2fea6?source=rss-fd526b7a7666------2</link>
            <guid isPermaLink="false">https://medium.com/p/8c385aa2fea6</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[webpack]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[MESHILEYA Emmanuel Seun]]></dc:creator>
            <pubDate>Tue, 17 Apr 2018 12:37:16 GMT</pubDate>
            <atom:updated>2018-04-17T16:03:48.469Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*-Nd4WoGNdHt6O0Xm-cYlMA.jpeg" /></figure><p>In case you missed the <strong>part 1</strong> of the series, you can do well to check it <a href="https://medium.com/@meshileya/a-forum-engine-using-nodejs-and-vuejs-10b5939ebf2e"><strong>here</strong></a>. The part 2 would be more fun as we walk through the structure of the project.</p><p>Grab a seat with a cup of coffee. clone the project <a href="https://github.com/meshboy/forum-engine-api/tree/project-walk-through"><strong>here</strong></a> to make it easier to access for better understanding as we progress.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/275/0*rDOv-nEqLyHeQ9da." /></figure><p>Overtime, I have structured theproject as much as I think it would be pretty simple for someone to understand at a glance. I’m a fan of Model-View-Controller (separation of concerns). This design pattern ensures the project is well maintained. Now we have got a router (ensures easy access to handle client request).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*E4I2Rb95e1QWGlCVgtPOkg.png" /><figcaption>Model-Router-Controller</figcaption></figure><p>In <strong>part 1</strong>, we created a basic router. The browser(http://localhost:3000) served as the client which sends a request to the application, the request was then handled by the router by returning the appropriate result.</p><pre>app.get(&#39;/&#39;, (req, res) =&gt; {<br><br>  res.json({ status: &#39;Introduction to Nodejs&#39; })<br>})</pre><p>Here we go,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/313/1*DckihboAIjbrt4Re-E4HMQ.png" /><figcaption>Project Structure</figcaption></figure><p><strong>Project breakdown</strong></p><ul><li><strong>Webpack</strong> : Well, we are in the 24th century, Webpack is not only used for the client side anymore. We would be using es6 and above and guess what ? The project is most likely to break depending on where its being hosted. The best use-case for us is to convert it back to es5. Webpack does that in a flash. One of the most interesting thing about Webpack is the state its maintains when a file is changed. In Nodejs, the moment you edit any file in the project, the application has to be restarted for the change(s) to take place. The <strong>webpack.testing.js</strong> file handles the test part of the project.</li></ul><pre>webpack.HotModuleReplacementPlugin()</pre><p>The above snippet performs <strong>hot reload </strong>once it smells a change in the project. To understand more on how we would be using Webpack in this project, please check <a href="https://hackernoon.com/hot-reload-all-the-things-ec0fed8ab0">here</a>.</p><ul><li><strong>test</strong>: The test folder contains two files (api.test.js and helper). The api.test would handle test(s) that ensure the routes work as expected while the helper contains modules we would be using in api.test.</li><li><strong>middleware</strong>: Nothing is safe but we can make it very hard to crack. The middleware is the wall to the apis. Once a request is made, it hits the middleware before the resource is being fetched.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/825/1*LtYZQTBk-Hel-7yhGXOSFQ.png" /><figcaption>middleware based architecture</figcaption></figure><ul><li><strong>config</strong>: The config handles how the application responds to different environment (production, development or local).</li><li><strong>modules</strong>: This contains independent units.</li></ul><p><em>auth</em> : This contains logic of how to verify a client, sign in a client etc.</p><p><em>errorHandler</em>: we would always run into errors but its very important to handle them well. errorHandler ensures handled errors are well interpreted.</p><p><em>query</em>: A generic CRUD (Create, Read, Update and Delete) that serves the controller based on request.</p><p>query.test.js: Yeah, we need to test the sub-module (query) to ensure its working as expected.</p><ul><li><strong>router</strong>: handles all the routes we have in the resource. we can call him a base router.</li><li><strong>resources</strong>: Since we are trying to create a forum api. We would only need two resources which are user and post.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/314/1*c1bKNDqEyzSOKwYXiWU4Vw.png" /><figcaption>resources</figcaption></figure><p>For each resource, the separation of concern plays a major role here. A resource contains a model, controller and a router. Personally, I believe this structure makes it easier to spot an error, test and easy to read or understand.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/0*UhewCbpZlluIPXx0.gif" /></figure><p>Once we can familiarize ourselves with the way the project is structured and what each file is meant for. I believe we are ready to take a full dive and get our hands dirty.</p><p>Lest I forget, the <strong>package.json</strong> file has been updated to handle all the dependencies we would be using for the application.</p><pre>{<br>  &quot;name&quot;: &quot;forum-engine-api&quot;,<br>  &quot;version&quot;: &quot;1.0.0&quot;,<br>  &quot;description&quot;: &quot;Using nodejs to write Forum engine api.&quot;,<br>  &quot;author&quot;: &quot;Meshileya Seun &lt;meshileyaseun@gmail.com/&gt;&quot;,<br>  &quot;license&quot;: &quot;MIT&quot;,<br>  &quot;scripts&quot;: {<br>    &quot;start&quot;: &quot;webpack --progress --color&quot;,<br>    &quot;test:unit&quot;: &quot;NODE_ENV=testing rm -rf ./tmp/mocha-webpack &amp;&amp; mocha-webpack --webpack-config webpack.testing.js \&quot;src/**/*.test.js\&quot; --timeout 10000 --require source-map-support/register&quot;,<br>    &quot;test&quot;: &quot;yarn run test:unit&quot;<br>  },<br>  &quot;dependencies&quot;: {<br>    &quot;bcrypt&quot;: &quot;^1.0.3&quot;,<br>    &quot;express&quot;: &quot;^4.16.3&quot;,<br>    &quot;express-jwt&quot;: &quot;^5.3.0&quot;,<br>    &quot;jsonwebtoken&quot;: &quot;^8.1.0&quot;,<br>    &quot;lodash.merge&quot;: &quot;^4.6.0&quot;,<br>    &quot;mongoose&quot;: &quot;^4.12.1&quot;<br>  },<br>  &quot;devDependencies&quot;: {<br>    &quot;babel-core&quot;: &quot;^6.26.0&quot;,<br>    &quot;babel-loader&quot;: &quot;^7.1.2&quot;,<br>    &quot;babel-plugin-transform-regenerator&quot;: &quot;^6.26.0&quot;,<br>    &quot;babel-plugin-transform-runtime&quot;: &quot;^6.23.0&quot;,<br>    &quot;babel-preset-env&quot;: &quot;^1.6.0&quot;,<br>    &quot;babel-preset-stage-0&quot;: &quot;^6.24.1&quot;,<br>    &quot;chai&quot;: &quot;^4.1.2&quot;,<br>    &quot;chai-http&quot;: &quot;^3.0.0&quot;,<br>    &quot;mocha&quot;: &quot;^4.0.1&quot;,<br>    &quot;mocha-webpack&quot;: &quot;next&quot;,<br>    &quot;raw-loader&quot;: &quot;^0.5.1&quot;,<br>    &quot;source-map-support&quot;: &quot;^0.5.0&quot;,<br>    &quot;start-server-webpack-plugin&quot;: &quot;^2.2.0&quot;,<br>    &quot;webpack&quot;: &quot;^4.5.0&quot;,<br>    &quot;webpack-cli&quot;: &quot;^2.0.14&quot;,<br>    &quot;webpack-node-externals&quot;: &quot;^1.6.0&quot;<br>  }<br>}</pre><p>The modules should look familiar.. if not, as we proceed, there usefulness would be revealed.</p><pre>&quot;scripts&quot;: {<br>    &quot;start&quot;: &quot;webpack --colors --progress&quot;,<br>  }</pre><p>we have two custom scripts .. (start and test) . <strong>yarn start </strong>runs the custom script (start) with webpack using two arguments (colors and progress). The colors argument makes the output colorful while the progress arguments shows the percentage of file bundling.</p><p>Lets install the dependencies and run the project.</p><pre>yarn install &amp;&amp; start</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/996/1*yUT0c1MaCu49TQDYZqX8MQ.png" /></figure><p>Did you notice the folder dist created based on what we configured in webpack ? The<strong> dist</strong> folder contains all the bundled scripts.</p><p>Lastly, lets run the test script even though we are yet to write any test.</p><pre>&quot;scripts&quot;: {<br>    &quot;test:unit&quot;: NODE_ENV=testing rm -rf ./tmp/mocha-webpack &amp;&amp; mocha-             webpack --webpack-config webpack.testing.js \&quot;src/**/*.test.js\&quot; --timeout 10000 --require source-map-support/register&quot;,</pre><pre>&quot;test&quot;: &quot;yarn run test:unit<br>  <br>}</pre><p>The above run the test when time ≤10000sec, look for any file that ends with *.test.js using webpack.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_rx63NnPMhuW_RrE8wrX7w.png" /></figure><p>Obviously, no test has been written. So number of test pass would definitely be zero.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8c385aa2fea6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A forum engine using Nodejs and Vuejs (part 1)]]></title>
            <link>https://medium.com/@meshileya/a-forum-engine-using-nodejs-and-vuejs-10b5939ebf2e?source=rss-fd526b7a7666------2</link>
            <guid isPermaLink="false">https://medium.com/p/10b5939ebf2e</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[yarn]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[MESHILEYA Emmanuel Seun]]></dc:creator>
            <pubDate>Wed, 11 Apr 2018 09:19:00 GMT</pubDate>
            <atom:updated>2018-05-19T09:30:43.227Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*M-zp0vorxsUNlokY.jpg" /></figure><p>I was inspired by a colleague of mine to start writing a medium post. I got addicted to procrastinating until I finally made up my mind to put up my first medium post.</p><p>What came to my mind was to build a very <strong>simple forum</strong> (which involves user on-boarding and ability to create and comment on posts) using Nodejs on the server side and Vuejs on the client side. This project would be in series starting from the server side down to the client side.</p><p>At the end of the series, you would comfortably write apis in Nodejs using <strong>Test-Driven Development </strong>(build to make test pass)<strong> </strong>approach and consume apis with a simple forum interface in Vuejs</p><p>The series for the server side is broken down into the following outlines:</p><ol><li><em>Introduction</em></li><li><em>Project structure walkthrough</em></li><li><em>Setup mongodb with mongoose</em></li><li><em>Setup crud controllers</em></li><li><em>Routing</em></li></ol><p><strong>Why Nodejs ?</strong></p><p>Nodejs is a javascript server side framework. It is asynchronous in nature which means it does not need to wait for a current task to end before it processes another one, it can always go back to wrap up its unfinished tasks.</p><blockquote><em>As long as there’s something left to do, Node’s event loop will keep spinning. Whenever an event occurs, Node invokes any callbacks (event handlers) that are listening for that event.</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*y5M0ZSG8cebNLWh_lBf4wg.png" /><figcaption>How Nodejs application runs (<strong>source</strong>: Node.js the Right Way<strong>)</strong></figcaption></figure><p>There are popular frameworks for building server side applications with nodejs. Here are a few common ones:</p><p>* Connect</p><p>* Express</p><p>* Koa</p><p>* Hapi</p><p>* Sails etc</p><p>In this project, we would be using the Express framework because its easy for creating APIs with node. Also, other frameworks are dependent on express, its pretty much easy to use and easy to test.</p><p>Here is a simple Nodejs with express application in a file named server.js</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjsfiddle.net%2Fmeshboy%2Fj9uke063%2Fembedded%2F&amp;url=https%3A%2F%2Fjsfiddle.net%2Fmeshboy%2Fj9uke063%2F1%2F&amp;image=https%3A%2F%2Fwww.gravatar.com%2Favatar%2Fa37105cffba0c1b4fac76a64948e73ee%2F%3Fdefault%3D%26s%3D120&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=jsfiddle" width="600" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/4abf855000fa72d10c6af4e0f33fdc01/href">https://medium.com/media/4abf855000fa72d10c6af4e0f33fdc01/href</a></iframe><p>If the above code looks strange, don’t worry it would be a piece of cake as we continue in the series. To run the above application, you need to have Node.js installed. Check <a href="https://medium.com/@meshileya/a-forum-engine-using-nodejs-and-vuejs-10b5939ebf2e">here</a> on how to install. Fire up your terminal or cmd, navigate to your current working directory.</p><p>Type the following in your terminal or cmd (command prompt) and hit enter</p><pre>nodejs server.js</pre><p>Boom!… here it comes … An error is thrown because the express module that is required is yet to be added to the project.</p><pre>Error: Cannot find module &#39;express&#39;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*JuIBNE2P8WBq_S_1l7ZwzQ.gif" /></figure><p><strong>How to install module in Nodejs</strong></p><p>One of the easiest ways to install modules in Nodejs is to use a package manager. There are two main package managers out there (<strong>npm</strong> and <strong>yarn) . </strong>I have always been a fan of <strong>npm</strong> but in this project we would be making use of <strong>yarn</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*-oKztA7TWY2E-n1_zOswNQ.gif" /><figcaption>npm5 and yarn can relatively be used in place of each other.</figcaption></figure><p>Yeepi, I assumed you’ve installed yarn. Time to bring up express into our project.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/785/1*xcwsqPy_WqUYWWUYXTLbPg.png" /></figure><p>Boom … we now have express in our project. Also, yarn automatically adds <strong>package.json </strong>and <strong>yarn.lock</strong> files into our project. Is that not awesome ?</p><p>why do we need a package.json file ?</p><p>The package.json file contains our dependencies or modules we would be using in our project. It makes it easier to install all dependencies or modules especially when collaborating on a project.</p><pre>yarn install<br></pre><p>The above command installs all the modules it could see in the package.json file. Try and delete the node_modules and run the above command. It would automatically include the express module.</p><p>package.json</p><pre>{<br>  &quot;dependencies&quot;: {<br>    &quot;express&quot;: &quot;^4.16.3&quot;<br>  }<br>}</pre><p>Lets make our package.json to be more descriptive.</p><pre>{<br>  &quot;name&quot;: &quot;forum-engine-api&quot;,<br>  &quot;version&quot;: &quot;1.0.0&quot;,<br>  &quot;description&quot;: &quot;Using nodejs to write Forum engine api&quot;,<br>  &quot;author&quot;: &quot;Meshileya Seun &lt;meshileyaseun@gmail.com/&gt;&quot;,<br>  &quot;dependencies&quot;: {<br>    &quot;express&quot;: &quot;^4.16.3&quot;<br>  },<br>  &quot;devDependencies&quot;: {<br><br>  }<br>}</pre><p><strong>devDependencies</strong> is like our friend that is always ready to help us with our house chores. It would contain the modules that are needed to develop our api engine e.g test dependencies while <strong>dependencies</strong> modules are needed for the engine to run e.g express.</p><p><strong>yarn.lock</strong></p><p>Yarn lock makes sure nothing breaks in the future when installing the dependencies. Take for example, the above express dependency is <strong>^4.16.3. </strong>In the near future, it can be 4.16.4 and so on because the version specified allows future installation. Check <a href="https://yarnpkg.com/lang/en/docs/dependency-versions/">here</a> to understand more about dependencies range.</p><p>In the near future, the express module available is 5.0.0, upon installation the app stops working because of unforeseen errors.</p><p>This is where yarn lock comes in. It makes sure there is <strong>consistent module resolution</strong>.</p><p>Now lets run our first program again …</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/614/1*Pgv76AEhtkM4p6942ZJ-qA.png" /><figcaption>the application being fired up</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*ac61f3NiqgSNcA3aur6dSg.gif" /></figure><p>Incase port 3000 is not available, feel free to change the port to any port of your choice (5000, 4000, etc).</p><p>Yeah, we have successfully run our first program in Node. Now, lets access our program running on port 3000</p><pre>http://localhost:3000</pre><p>Tada!!!….</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/762/1*C0DnQCF83rMETLc3uVfPug.png" /><figcaption>Application accessed on port 3000</figcaption></figure><p>Lastly, we can also use <strong>yarn</strong> to run our application.</p><pre>yarn start</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/677/1*XpydU71drdYF9ZqsSBDEsA.png" /></figure><p>The source code of the application can be found <a href="https://github.com/meshboy/forum-engine-api">here</a>.</p><p>If you have any question, kindly hit the response. I would always respond. The next part can be found <a href="https://medium.com/@meshileya/a-forum-engine-using-nodejs-and-vuejs-part-2-8c385aa2fea6">here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=10b5939ebf2e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>