Survive APIs with these Simple Node_785

More people all over the world are learning about Node js. as it is quickly and consistently gaining in popularity. This is because the high demand for a web application framework that is event driven and also asynchronous. It is the amazing speed that many people are in need of and look out for whenever they are in search of a framework from where to build their apps. One of the most amazing advantages of Node.js is that you can speed up the way that it works, and make if work much faster than any other framework. You can achieve just about anything with Node.js. Here are some hacks to consider when you are planning your next app:

Execute Your API Calls in Parallel

When you are building a web app, you have to make a large number of internal API calls in order to gather all the data that you need for the web app creation. The problem is that these calls take time since each call has to complete before you can execute the other one. You may experience substantial frustration if you have several other calls to make. To avoid such delays, you can always execute these calls in parallel so that you will not have to wait for one to complete before you can execute the other one. Due to its asynchronous nature, Node.js is very efficient in running more than one function at the same time in parallel. As long as the functions do not depend on each other, you can always do this in order to speed up things. When they run parallel, they run faster and you might complete web app creation faster than you had expected.

In order to achieve this, you will have to use async.js. This is a module that Node uses in order to tame asynchronous.  Below is an example of how such functions can run in parallel using this model:

function runInParallel() {
async.parallel([
getUserProfile,
getRecentActivity,
getSubscriptions,
getNotifications
], function(err, results) {
//This callback runs when all the functions complete
});
}

1

Cache Data to Improve its Performance

When using Node.js and you realize that there is data you are trying to fetch that does not change too much, you can use cache in order to improve its performance. Below is an example:

var router = express.Router();

router.route('/latestPosts').get(function(req, res) {
Post.getLatest(function(err, posts) {
if (err) {
throw err;
}

res.render('posts', { posts: posts });
});
});

For instance, you create blog posts that you do not publish too frequently, you can cache their arrays, then you can clear the cache afterwards. This way, they will be published with ease.

The module you can use in this case is Redis, meaning that you must install Redis onto your machine. Once it is installed, use a client node_redis to store the pairs that are key or those that have great value. The following code illustrates how you can cache such posts:

ar redis = require('redis'),
client = redis.createClient(null, null, { detect_buffers: true }),
router = express.Router();

router.route('/latestPosts').get(function(req,res){
client.get('posts', function (err, posts) {
if (posts) {
return res.render('posts', { posts: JSON.parse(posts) });
}

Post.getLatest(function(err, posts) {
if (err) {
throw err;
}

client.set('posts', JSON.stringify(posts));
res.render('posts', { posts: posts });
});
});
});

The GZip Compression

This is another way through which you can greatly improve your web app performance. For it to work, turn the gzip compression on. What happens in this case is that the server will always compress any resource before sending it to the browser, making it easier for the browser to fetch this resource. In the end, you will not experience any delays of any kind. If you are dealing with a large data resource, you can be sure that it will be easier to fetch once it has been compressed.

The compression middleware will be used in this case to compress and also to serve the resource that has been compressed. If you are using the Express app, there is a built in middleware, express.static(), that you can use too.

Below is a code that shows how this can be done:

var compression = require('compression');

app.use(compression()); //use compression
app.use(express.static(path.join(__dirname, 'public')));

Optimize Your Queries in Order to Speed Them Up

In case of a query, it might take a long time for you to get a response. This is because the fetch function fetches through all the fields in an object, and there could be a large number of fields in one object, some of which may not be required in the search. If you are able to exclude some of these fields from the search, it could take a shorter period of time since there will be fewer fields included in the search.

To achieve this, you can optimize the query. The code below illustrates how this can be done:

Post.find().limit(10).exclude('comments').exec(function(err, posts) {
//send posts to client
});

Use of Nodemon

Having to run node [file].js over and over again when you start Node.js development can be very tiring. This takes up so much of your time and can be very frustrating. Nodemon is a great tool that can help you solve this problem once and for good. What you do is to install this tool by running npm install -g nodemon. Once it is installed, you can always run Node.js scripts via Nodemon.js. Use of Nodemon is in fact the best way to handle all Node.js developments and it improves the speed exceptionally, so you will not experience any delays.
2

An easier way to Debug Node.js Apps

Debugging Node.js apps can be quite confusing, even for those people who have used the most technical programming languages. Node.js comes with a built in debugger that you can easily use whenever there is need to debug apps. Alternatively, there is node-inspector; this is a debugger interface for Node.js that uses Blink Developer Tools. This is a much better option whenever you want to debug apps.

Node-inspector allows you to perform more than one function on your app, for instance changing codes, step debugging among others.
3

Using Node.js will make it easier for you to work faster, and like a pro. Using these hacks will help you troubleshoot, and transform your entire app developing experience.