In this article we’ll take a look at NodeJS for mobile apps BackEnd from different points of view.
We’ll highlight the next topics:
- NodeJS and its history
- Why you should use NodeJS – pros and cons
You’ll get both upsides and downsides of this tool and, of course, practical advices on how to build a mobile app BackEnd with NodeJS!
Recently this practically all-purpose tool stepped into the reign of servers.
We’d like to tell you why (and, what’s more important, when) you should use NodeJS for your BackEnd. For this purpose we’ll list its upsides and downsides.
NodeJS uses non-blocking I/O model
It basically means that all the operations that require communication with external systems (e.g. databases, file systems, etc) are non-blocking.
In a nutshell, the app continues to run while it’s waiting for this communication to complete.
Here’s an example: Say, you need to read 2 big files and it takes 5 seconds to read each.
If you use NodeJS asynchronous file operations, reading both files might (and most likely will) take less than 10 seconds, as both operations will run in parallel.
In fact, If you read them from different sources, it might even take just a notch more than 5 seconds!
A lot of mobile app servers tend to use social media authentication, and this can often be represented as an I/O operation That’s a good reason to create your mobile app with NodeJS backend.
Apps that use social media usually store user data in databases, save user files, send out push notifications, etc. But basically all these are simple input/output operations!
And guess what? NodeJS works well with I/O operations out of the box!
NodeJS has a lot of community-supported packages.
Do you need to process images? Easyimage can simplify this task a lot.
All above is only a small fraction of all the packages available on npm and they are multiplying daily!
Mobile app BackEnd development can greatly benefit from all kinds of these modules!
NodeJS is still “green”
NodeJS is quite a young environment and it’s developing even while you read this article!
It has an amazing opportunity not to make the same mistakes twice, based on the other environments experience. And you know what? Seems like NodeJS does a good job with that!
NodeJS is based on Chrome V8.
Thanks to the fast growing community, Chrome V8 is regularly updated, which constantly brings in new features and bug fixes.
NodeJS, of course, is not a silver bullet, but it’s a powerful tool worth taking a look at!
Nevertheless it has a bunch of downsides, making NodeJS not the best choice for some kinds of projects.
NodeJS is single-threaded
Most of the applications nowadays are multi-threaded. This allows separating calculations and processing new requests from the outside into different threads, that are running in parallel.
Workarounds for the problem
However a lot of companies implement their mobile projects in a way that their server can often off-load nearly all the calculations either to a database or to external commands (like easyimage) or to a reverse proxy (for example, nginx) to do encryption, etc
In case you want to process push-notifications, you can start a second instance of your app to purely process all the push-notifications generated by the first instance and then make the instances communicate through a database.
You can also run a bunch of NodeJS processes, and hide them behind a reverse proxy server – this often solves the problem of not using all of the multi-core system’s resources.
In addition, you can use cluster module, which has been recently added to help fixing this problem.
Nevertheless, NodeJS works its way around this problem. Guidelines like OWASP give detailed explanation of the vulnerabilities. We also recommend using so-called strict mode (as it eliminates lots of deprecated and vulnerable features).
Eval is evil
Finally, you should minimize the use of eval. Of course, it can be useful sometimes, but most of the time you can perfectly manage without it. This tip will be most useful for mobile app backend developers – it’s gonna help both boosting speed and increasing security of their code.
To sum it up, NodeJS can be used to write secure, fast and readable code, yet it does require some experience to do so.
NodeJS requires a VPS
NodeJS is harder to deploy than a traditional server because of the fact that it needs a VPS.
There is no standardized way to deploy NodeJS. Plus, if you do not monitor it, it may simply crash from an exception (then it will stop replying at all).
Fortunately there already are some good solutions (Forever is a prominent example). However all of them require some setup.
Besides, it’s a bit hard to install on the server correctly, and it will run as root by default. That’s why you will have to lower the privileges programmatically from inside, and nvm can help with versioning and updating your installation.
To top it off, it’s practically impossible to find Node.JS hosting similar to traditional PHP hosting. There are services like Heroku, but it’s way closer to a VPS than to PHP hosting.
To decide whether using NodeJS is a good or a bad idea for you, try answering the following questions:
- Can you (your developer) offload most (better all) of the calculations to external services (database, other software, file system, etc)?
- Do your developers have enough experience?
- Do you have a system administrator to setup a VPS? If not, are you ready to go with Heroku or a similar service as your hosting?
If you can firmly answer “yes” to all three questions, then Node.JS is the right choice!
If you have any questions regarding Node.JS usage for your project feel free to contact us, and you’ll never regret it!