Evaluate Weigh the pros and cons of technologies, products and projects you are considering.

Benefits of developing with Node.js in AWS

While the Node.js JavaScript-based language is still in beta, many programmers turn to it to simplify and quicken functions in the AWS cloud.

JavaScript's Node.js has become popular with cloud-based programmers because it allows them to write one language...

for both server-side and client-side applications. JavaScript's asynchronous nature and different patterns for handling event-driven and responsive programs makes it easy to get hooked into this new language.

Node.js is still in beta; however, many developers use it in production to handle a lot of traffic and a multitude of workflows.

So, why learn Node.js? Is it worth it to make the leap to the new programming language? Here's a look at a few of the benefits of developing with Node.js.

Fast and familiar coding

Node.js is entirely JavaScript; any work that front-end developers did can also be done for server code. Developers can use the exact same patterns and -- in some cases -- frameworks, with both back-end and front-end development.

The additions of Node Package Manager (NPM) and Bower for front-end packages make it easy to quickly develop applications in Node.js and import existing libraries. This made the language more popular and led to a huge influx of full-stack developers who write both front-end and back-end code.

A better default pattern

With Node.js, developers must use a callback pattern. This often makes it easier to handle services that don't really need to happen sequentially. For example, in Python you may write something like this:

def handleRequest(self, request):
  trackRequest(request) # Waits for return
  return 'Ok, you can come in'

The same code would look like this in Node.js:

function handleRequest(request, response){
  tracRequestAsync(request); // Immediately returns
  response.send(‘Ok, you can come in');

In the Python example, the trackRequest method doesn't seem to take long. But in the Node.js version, that trackRequest function would be asynchronous. If you're waiting for a server to return something, you don't need to hold up the end user in the process. You can do multiple things in response to an end user's request that aren't needed before you can notify the user that it's ready.

While this process is available in Python, libraries are not natively asynchronous. Therefore, developers must do a lot of wrapping around function calls, which can be difficult. Node.js is asynchronous from the language level up.

Callbacks and promises

It takes most developers a little while to learn the JavaScript concept known as promises. Generally, asynchronous JavaScript functions take callbacks that look like this:

request(options, function(response){
  // Work with the response
});

The promise pattern suggests asynchronous functions return a promise object; this object is similar to a deferred response in other languages, which contain a then function that takes a callback.

request(options).then(function(response){
 
  // Work with the response
 
});

In both cases, the response is immediately returned, so the next line of code fires while the user waits on a response from the server. However, the promise method makes it easier to change up multiple concurrent dependencies.

Q.all([
  request(options),
  request(options2),
  request(thirdSetOfOptions),
]).then(function(responses){
  // Work with all the responses

In this scenario, the user initiates three simultaneous requests and waits for all of them to return responses before running the user's logic, such as returning results. This makes it simple to manage multiple tasks, while still requiring each to complete before returning to the user.

Next Steps

How do I manage and monitor AWS Lambda code?

AWS CodeDeploy reaches into the data center

Use Git to version-control AWS app development

This was last published in July 2015

PRO+

Content

Find more PRO+ content and other member only offers, here.

Join the conversation

1 comment

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

Does your enterprise plan to use Node.js code in app development? Why or why not?
Cancel

-ADS BY GOOGLE

SearchCloudApplications

TheServerSide.com

SearchSoftwareQuality

SearchCloudComputing

Close