Skip to content

Instantly share code, notes, and snippets.

@ninjascribble
Last active March 4, 2019 20:36
Show Gist options
  • Save ninjascribble/cf72d16f5f96b8c4dabe to your computer and use it in GitHub Desktop.
Save ninjascribble/cf72d16f5f96b8c4dabe to your computer and use it in GitHub Desktop.
Introduction to NodeJS

Introduction to NodeJS

Resources

JavaScript language reference

https://developer.mozilla.org/en-US/docs/Web/JavaScript

Mozilla Developer Network has the best native JavaScript language spec on the web right now. It's up to date with practical examples and easy to navigate.

NodeJS libarary reference

http://nodejs.org/api

For NodeJS-specific reference you can't beat the source. The documentation can be difficult to navigate until you know what you're looking for, but the packages are purposefully named and the documentation provides a good sense of how things are intended to be used.

JS: The Right Way

http://jstherightway.org/

A collection of demos, tutorials, articles and more all geared towards building well-thought-out and reliable JavaScript applications.

REST Frameworks

Out of the many out there, Express and Koa are the two big players. Geddy is also a viable option, providing a Rails-like command-line application for getting your scaffolding out of the way. All three are somewhat opinionated about the way your project should be structured, and a good decision would be based on how well those opinions align with internal opinions.

Express

http://expressjs.com

Easily the most widely-used and supported of the three. Express is built on top of Connect and supports any library that was written with Connect in mind (everything from database connectors to custom authentication middleware).

Pros

  • Generator sets up a minimal working REST app.
  • Works with all of the Connect middleware libraries on NPM.
  • Library-oriented with no assumptions about technical stack.
  • Active and supportive community.

Cons

  • No built-in utilities for common tasks like connecting to a database.

Koa

http://koajs.com

Koa is relatively new compared to the other two. It differentiates itself by offering built-in support for ES6 features (e.g. Pipes, Generators). ES6 must be enabled through the NodeJS Harmony flag, which exposes language features that are still in definition and therefore subject to change.

Pros

  • Built by the same team that built Express.
  • Takes advantage of fundamental upcoming language features.

Cons

  • No built-in utilities for common tasks like connecting to a database.
  • Less community support than Express.

Geddy

http://geddyjs.org

More rails-like than the other two, Geddy comes with a command-line tool for building out your routes, models, views and controlers. Geddy is a fully-featured development framework with out-of-the-box i18n support, and built-in adapters for working with MySQL, PostgreSQL and MongoDB.

Pros

  • Command-line utility for setting up scaffolding and registering new routes, views and models.
  • Fanstastic documentation with plenty of useful examples.
  • Built-in support for common tasks like connecting to a database.

Cons

  • Highly opinionated; can be difficult to break out of expected archetectural patterns.
  • Custom middleware with far less support than Connect/Express.

Build Systems

Grunt

Link: http://gruntjs.com

A gruntfile is basically a collection of loosely tied-together scripts. There are a bunch of libraries that support running tests, building coverage reports, deploying to various environments, etc... but what makes it powerful is its flexibility. Targets are just JavaScript and don't have to take into consideration that they're being run by Grunt.

Gulp

Link: http://gulpjs.com

Gulp is being billed as a successor to grunt. It can be really easy to get started if you can find the right libraries, but building custom libraries is more involved than with Grunt. The project's owners are opinionated, which means that gulpfiles tend to be more terse and easier to understand than gruntfiles. However it also means that you're stuck with decisions that you might not like (e.g. each target may only have one dependency).

Unit Testing

Mocha

Link: http://visionmedia.github.io/mocha

Mocha can run TDD and/or BDD-style tests on the command-line, in your browser, or as generated files that can be consumed by other applications. WebStorm and IntelliJ both have native support.

Chai

Link: http://chaijs.com

Chai is an assertion library that supports should, expects and assert syntax.

Nock

Link: http://github.com/pgte/nock

Nock will help you mock class dependencies.

Recommended Libraries

Lo-Dash

Link: http://lodash.com

Lo-Dash is the successor to Underscore which is still billed as the missing JavaScript utils package. Lo-Dash is well-documented, super-fast, and provides simple APIs for what would ordinarily be complex tasks in JavaScript (e.g. deep-copying objects).

Bluebird

Link: https://github.com/petkaantonov/bluebird

NodeJS is an event-driven language. Many operations will run asychronously, and often rely on callback functions to send a result to. Promises offer a way to avoid nested callbacks which can make your code base difficult to read and add a lot of unwanted coupling. Bluebird is a lightning-fast implementation of the A+ Promises specification.

Socket.io

Link: http://socket.io

The gold-standard NodeJS websockets implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment