Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Why I Love Node.js: Promises, Express, and the CLI

DZone's Guide to

Why I Love Node.js: Promises, Express, and the CLI

I published a blog post about Node.js a couple of years ago. After recently using it for some more crazy stuff, I thought I’d post more reasons I love Node.js!

· Web Dev Zone
Free Resource

Add user login and MFA to your next project in minutes. Create a free Okta developer account, drop in one of our SDKs to your application and get back to building.

I published a blog post about Node.js a couple of years ago. After recently using it for some more crazy stuff, I thought I’d post more reasons I love Node.js!

Promises, Promises

I mentioned bracket hell in my previous post. Well, in the intervening period, I’ve started using Promises for a lot of things, especially the Q promises library in npm.

Promises allow you to easily chain actions together. You can run things in parallel and wait for them to finish, or run things in sequence, or both! I’ve just used both in the last week to start 10 'threads' using Q.all, with each thread running 1500 tasks in series using promisesArray.reduce.

This allowed me to test a customer’s Node.js Express based web application at scale with just a few lines of code, which in turn tested high throughput ingest and query loads in MarkLogic Server.

Express Web Apps

Creating a web server sounds scary, but with Express is very, very easy. You just intercept the small number of routes (URL patterns) you need, and you’re away!

You can even use the URL pattern itself to infer parameters. E.g. /get/fred/1 could be used to fetch the first document in the fred collection. Pretty cool, and reminiscent of Ruby on Rails.

I’d definitely recommend learning Express first if developing a web application.

Command Line Applications

Did you know you can even use Node to create an NPM package that installs a global application on any platform? This is awesome in NPM form!

I’ve used this to package up two tools I use on projects in to my mlnodetools package. mljsadmin allows you to capture a MarkLogic application with all server settings and transfer it to another machine with just a couple of commands. mljsserve uses Express mentioned above to create a thin web server which also proxies MarkLogic REST API URLs directly to MarkLogic Server.

This is great for demonstration web apps at work in MarkLogic Sales Engineering (We’re hiring by the way, so please contact me if you’re interested!)

I use a few NPM packages for mljsadmin:-

  • minimist is used to parse command line arguments. I can’t express my love for this capability enough!
  • q again is used for promises when chaining complex functionality together (so 'install' runs about 9 sub-commands in series, which can also each be ran on their own from the command line)
  • winston is used for logging output and errors to both the command line (info level) and a file (debug level). I use this in my mljs MarkLogic REST API wrapper too.
  • istextorbinary allows me to test files to see if they are binaries. Useful as MarkLogic Server doesn’t hold every mime type in use. I use this to determine whether I need to do a binary upload or not, and what mime type to specify
  • terminal-kit is used to provide color coded and styled text on the command line output. Useful so that I can have green for successes, yellow for warnings, and red for major errors.

Summary

I’m constantly and pleasantly surprised that for whatever little tool or demonstration I need, no matter how complex, Node.js greatly simplifies my life!

Node.js' event driven nature and wide support for extension via NPM allows me to quickly find and make use of even the most esoteric functionality I need. I often have a choice of esoteric libraries!

I’d recommend learning Node.js to anyone working in IT today. Node.js can make your projects fly!

Launch your application faster with Okta’s user management API. Register today for the free forever developer edition!

Topics:
nodejs ,javascript ,npm ,rest

Published at DZone with permission of Adam Fowler, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}