<?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 Bernard Tolosa on Medium]]></title>
        <description><![CDATA[Stories by Bernard Tolosa on Medium]]></description>
        <link>https://medium.com/@bernardotolosajr?source=rss-383651d7098b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*NBCIp3erfNbQ-GjGbqDICQ.jpeg</url>
            <title>Stories by Bernard Tolosa on Medium</title>
            <link>https://medium.com/@bernardotolosajr?source=rss-383651d7098b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 12:20:00 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bernardotolosajr/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[Building Node.js backend API using egg framework with rethinkdb]]></title>
            <link>https://medium.com/@bernardotolosajr/building-node-js-backend-api-using-egg-framework-with-rethinkdb-5c705969511?source=rss-383651d7098b------2</link>
            <guid isPermaLink="false">https://medium.com/p/5c705969511</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[Bernard Tolosa]]></dc:creator>
            <pubDate>Wed, 15 Aug 2018 15:50:23 GMT</pubDate>
            <atom:updated>2018-08-16T14:15:04.959Z</atom:updated>
            <content:encoded><![CDATA[<p>Building Node.js backend API using egg framework with rethinkdb</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8D4CKggSytSWRHWwyeU0gQ.png" /></figure><p>egg is a convention-over-configuration framework base on koa.js, out of the box egg already baked in most of the things we need, its also provide a high extensible plugins like <a href="https://github.com/eggjs/egg-mysql">egg-mysql</a> and <a href="https://eggjs.org/en/plugins/">more</a>. This means we can focus on writing functionality.</p><p>You can read more about egg <a href="https://eggjs.org/en/intro/index.html">here</a></p><p>Lets see how easy it is to build a backend API using egg.</p><h4>Prerequisites</h4><ul><li>Operating System: Linux, OS X or Windows.</li><li>Node.js Runtime: 8.x or newer; it is recommended that you use <a href="http://nodejs.org/">LTS Releases</a>.</li><li>RethinkDB</li></ul><h3><strong>Installation</strong></h3><p>egg provided a cli for scaffolding the project.</p><pre>npm i egg-init -g</pre><p>Lets create the project called egg-smashers-api, it can create, read, update and delete the smashers. We are using rethinkdb as a database.</p><p>Read more about <a href="https://www.rethinkdb.com/">rethinkdb</a></p><pre>egg-init egg-smashers-api --type=simple</pre><p>It will create the project structure like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yWjLqz5wA87PIGeqs7py2w.png" /></figure><p>Now lets smoke test our new created application, go the the application we created and run the <strong>npm run dev</strong> and visit the <strong>http://localhost:7001.</strong> If everything is working you should see the “hi, egg” message.</p><pre>cd egg-smashers-api</pre><pre>npm run dev</pre><pre>open localhost:7001</pre><p><strong>The Database</strong></p><p>For the rethinkdb we are using the <a href="https://github.com/neumino/rethinkdbdash">rethinkdbdash</a> Node.js driver. and to make it more easy we are using the <a href="https://github.com/BernardTolosajr/egg-rethinkdbdash">egg-rethinkdbdash</a> plugin, its a egg plugin i created.</p><p>Lets install the plugin and configure it.</p><pre>npm i egg-rethinkdbdash --save</pre><p>To enable the rethinkdbdash plugin, edit the config/plugin.js</p><pre>exports.rethinkdbdash = {<br>   enable: true,<br>   package: &#39;egg-rethinkdbdash&#39;<br>}</pre><p>To add the rethinkdb config, edit the config/config.default.js</p><pre>config.rethinkdbdash = {<br>  client: {<br>    host: &#39;localhost&#39;,<br>  }<br>}</pre><p><strong>Database table</strong></p><p>One last thing lets add the database table, by default rethinkdb will create the table in the <strong>test</strong> database.</p><p>For this example we can do this in the egg custom startup configuration. Add the new file app.js in the root of the project.</p><p>You can read more about application startup config <a href="https://eggjs.org/en/basics/app-start.html">here</a></p><pre>// root/app.js</pre><pre>module.exports = app =&gt; {<br>  app.beforeStart(async () =&gt; { <br>     // Note: this will fail if the table already exist,<br>     // maybe you can check if the table if already exist or <br>     // move this table creation somewhere.</pre><pre>     await app.rethinkdbdash.tableCreate(&#39;smashers&#39;).run();<br>   }<br>}</pre><p><strong>The Controller</strong></p><p>Now lets create our smashers controller and implement the Create functionality.</p><pre>// controller/smashers.js</pre><pre>const Controller = require(&#39;egg&#39;).Controller;</pre><pre>class SmashersController extends Controller {<br>   async create() {<br>     const { name } = this.ctx.request.body;<br>     const result = await     this.app.rethinkdbdash.table(&#39;smashers&#39;).insert({ name }).run();</pre><pre>   }<br>}</pre><p>Lets add endpoint to the router to expose the create functionality, open app/router.js and add this.</p><pre>router.post(&#39;/smashers&#39;, controller.smashers.create)</pre><p>Read more about the <a href="https://eggjs.org/en/basics/router.html">router</a></p><p>We can test the endpoint manually using curl, because why not.</p><pre>curl -H &quot;Accept: application/json&quot; -H &quot;Content-type: application/json&quot; -X POST -d &#39;{&quot;name&quot;: &quot;mario&quot;}&#39; <a href="http://localhost:7002/channels">http://localhost:7001/</a>smashers</pre><p>Oops! not to excited you will see an error saying “missing csrf token”. Its enable by default, lets disable it. edit the/config/config.default.js and add this.</p><pre>// config/config.default.js</pre><pre>config.security =  {<br>  csrf: {<br>    enable: false,<br>  }<br>}</pre><p>Test it again! You should see something similar to the response below. Awesome we just created our first endpoint.</p><pre>{&quot;deleted&quot;:0,&quot;errors&quot;:0,&quot;generated_keys&quot;:[&quot;4dd09f4d-0d74-42c4-a151-a98b00cb27e9&quot;],&quot;inserted&quot;:1,&quot;replaced&quot;:0,&quot;skipped&quot;:0,&quot;unchanged&quot;:0}%</pre><p><strong>The read method</strong></p><pre>// controller/smashers.js</pre><pre>const Controller = require(&#39;egg&#39;).Controller;</pre><pre>class SmashersController extends Controller {<br>   ...<br>   async index() {<br>    const smashers = awaitthis.app.rethinkdbdash<br>          .table(&#39;smashers&#39;).run();</pre><pre>    this.ctx.body = smashers;<br>   } <br>}</pre><p>Expose the index function in the router.</p><pre>//app/router.js</pre><pre>router.get(&#39;/smashers&#39;, controller.smashers.index);</pre><p>Not lets get all the smashers!</p><pre>curl <a href="http://localhost:7002/smashers">http://localhost:7002/smashers</a></pre><pre>// [{&quot;id&quot;:&quot;4dd09f4d-0d74-42c4-a151-a98b00cb27e9&quot;,&quot;name&quot;:&quot;mario&quot;}]</pre><p><strong>The update method</strong></p><pre>// controller/smashers.js</pre><pre>const Controller = require(&#39;egg&#39;).Controller;</pre><pre>class SmashersController extends Controller {<br>   ...<br>   async update() {<br>      const id = this.ctx.params.id;<br>      const { name } = this.ctx.request.body;<br>      const result = await this.app.rethinkdbdash<br>          .table(&#39;smashers&#39;).get(id).update({ name })<br>          .run();</pre><pre>      this.ctx.body = result;<br>    } <br>}</pre><p>Expose the update function in the router.</p><pre>router.put(&#39;/smashers/:id&#39;, controller.smashers.update);</pre><p>Lets test it!</p><pre>curl -H &quot;Accept: application/json&quot; -H &quot;Content-type: application/json&quot; -X PUT -d &#39;{&quot;name&quot;: &quot;Super Mario&quot;}&#39; <a href="http://localhost:7001/smashers/4dd09f4d-0d74-42c4-a151-a98b00cb27e9">http://localhost:7001/smashers/</a>replace_here_the_id_you_get</pre><pre>// {&quot;deleted&quot;:0,&quot;errors&quot;:0,&quot;inserted&quot;:0,&quot;replaced&quot;:1,&quot;skipped&quot;:0,&quot;unchanged&quot;:0}</pre><p><strong>The delete method</strong></p><pre>// controller/smashers.js</pre><pre>const Controller = require(&#39;egg&#39;).Controller;</pre><pre>class SmashersController extends Controller {<br>   ...<br>   async update() {<br>      const id = this.ctx.params.id;<br>      const result = await this.app.rethinkdbdash<br>          .table(&#39;smashers&#39;).get(id).delete()<br>          .run();<br>      this.ctx.body = result;<br>    } <br>}</pre><p>Expose the update function in the router.</p><pre>router.delete(&#39;/smashers/:id&#39;, controller.smashers.delete);</pre><p>Lets test it!</p><pre>curl -H &quot;Accept: application/json&quot; -H &quot;Content-type: application/json&quot; -X DELETE <a href="http://localhost:7002/smashers/c1a07f0c-c0c4-41bb-a79b-fc69b07ccff4">http://localhost:7001/smashers/</a>your_get_id_here</pre><pre>// {&quot;deleted&quot;:1,&quot;errors&quot;:0,&quot;inserted&quot;:0,&quot;replaced&quot;:0,&quot;skipped&quot;:0,&quot;unchanged&quot;:0}</pre><p>Thats it! a very basic example of building API using egg and we just scratch the surface of the framework.</p><p>Don’t forget to share the love! Happy coding :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5c705969511" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>