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

Learn how to build modern digital experience apps with Crafter CMS. Download this eBook now. Brought to you in partnership with Crafter Software

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!

Crafter is a modern CMS platform for building modern websites and content-rich digital experiences. Download this eBook now. Brought to you in partnership with Crafter Software.

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.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}