Nodejs with KOA2 by Amin B Nagpure - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

Why it was hard to learn nodejs

 

We came with background with class oriented programming, from C#, Java, C, c++.

JavaScript is functional programming, and we never took JavaScript seriously, we just used it for website validation.

Why you had hard time learning nodejs, nodejs is rapidly evolving technology, framework changes happens almost every day, the code you might have got searching Google is old and absolute and no longer works, always check the date when that article was written, it was working then, but not working now, you were typing the same example code, but when you run it, it doesn’t work, it is most likely that example code is absolute.

Some  examples on the internet are using nodejs with Angularjs or Reactjs, that is making it more problematic to learn, you got to learn additional Angularjs framework and Reactjs framework and that confuses you, where to start what to do, it totally confuses you.

There is no need to learn Angularjs, Reactjs, for building apps with nodejs, nodejs with koa framework has everything required to make a full blown web app.

What these Angularjs, Reactjs  frameworks does, they add extra layer of complexity on the app, and you waste your time to figure it out, what happened, from where this error is coming, this is where I wasted my time, when I was learning.

Angularjs, and Reactjs framework, updates and change frequently, so when you try to implement there code, it doesn’t work, further increasing your frustration.

There tutorial nodejs with Angularjs are written by geeks who have no life, but to experiment and do some outstanding things in coding, and we refer these tutorials for basic learning and gets stomped.

Books available on the internet are old, outdated technology, check when it was updated, nodejs is changing very rapidly, if you buy these books with outdated techniques, it will add to your frustration.

Lot of Modules are interdependent on each other, if some modules get upgraded and you update it, it may make your code error prone, since the modules which were dependent on that module are not updated or changed completely, this is the real cause of frustration.

Each module is independently developed by geeks and they are dependent on other modules to function.

When I tried to use koa with generator function, I faced lot of problems, because some modules were abandoned by their creators and were no longer working, so I used KOA2 which has functionality like async and await much like C # code; it is implementing ECMASCRIPT 6 features.

I will also explain problems I faced and reasons behind and what I did to fix it.

Note: If you are totally new then you have to look for , what packages and package versions  you are using in project, don’t get confuse in packages, there are so many packages to use,

Example

For Database Connection:

co-monk easy, but mongoose is also good for creating schema and predefine functions For Mongodb database connection there is Monk, Co-Monk And mongoose:

I used monk

 

For parsing co-body and koa-bodyparser:

I found koa-bodyparser easy you have to call just app.user(bodyparser); for parsing data from pages and using ctx.request.body u will get data from forms, but in co-body you have to mention parse(this.body) for each route

For rendering koa-swig, koa-view, co-views:

I found koa-views better, just app.user(Views(‘foldername’,{map:{html:swig}}));

And you can render pages by calling this.render(‘pagename’);

 

For routing koa-route, koa-router:

Found koa-route better but it has some limitation but I didn’t found those yet

For templating, too many options are available swig, handlebars, underscore and many more

Found swig better and easy for me because you don’t have to do extra coding for it {%block%} is good like handlebars you can call {{datafromRoute}} and bind data in to html page{%if%}{%endif%} are easy to understand.

So choose one wisely and stick to it, after trying so many examples and tutorial finally I found some packages which are comfortable to work for me.

At starting I was too much confused between these packages and was unable to decide what to use.

For that I created separate projects for experimenting, which package is comfortable for me, and finally decided for my project, you might not agree with it you can use other packages.

This is where problem lies, which package to use and which to not, you will get some examples which use different packages to do some task, but may not work well together with alternate packages, this is where you will be wasting time, this is where you will get frustrated, that’s why I have mentioned which packages iam using.