Introduction to Node.js for web developers

Node.js is the JavaScript run-time environment on the server side. It uses open-sourced Chrome’s V8 engine for JavaScript execution. V8 is the same JavaScript engine that powers browsers like Opera and Vivaldi.

Concepts in Node.js

  • Node.js uses an event-driven, non-blocking I/O model. Functions like Querying Db, file system read/write operations are designed to be asynchronous non-blocking. Task completion will be signed through callbacks. Special care needs to be taken to avoid callback hell

  • Node.js is single threaded. You need to use process managers like pm2 to spin additional instance of your application to efficiently use the available cores on the server.

  • Node follows the Unix principle. One utility per file. Npm modules should be used like Lego blocks for building the software.

  • Validation logic, post records insertion logic are all written as middleware. It is Middleware not middlewares

JavaScript VM – Chrome’s V8

V8 engine, written in C++, is the only JavaScript VM that is currently being used. There are also proposals to use Microsoft’s Chakra core and Mozilla’s Spider Monkey. Once the options are viable, execution engine can be switched. This shouldn’t be a problem as the engine themselves will be implementing the TC39 standards. V8 compiles the JavaScript code directly to native machine code. There is no interpreting or compiling phase.

Dependency management – npm & bower

npm, a word play, is the dependency manager for Node.js. Dependencies are maintained in package.json and are installed in node_module folder.

Bower
It used for installing front-end assets like HTML, CSS, JS. The dependency details are maintained in bower.json and are installed in bower_components/ folder. These days, more frontend framework are preferring npm over bower for managing dependencies.

Frameworks

Express is the most popular, widely used framework in the Node land. It is partly, because it is the “E” of the “MEAN” stack. Also, it is unopinionated. You can use routes to handle request – response, controller for db model interaction. It is easy to set up and run with it.

There are also other notable frameworks such as Walmart’s hapi.js, Paypal’s kraken.js. If you’re coming from Rails background, you can check sails.js. It tries to solve the same problem that Rails had solved for Ruby.

Tool set – IDEs, Linting, task runners

Statically typed language have IDEs that will prevent you from making errors while coding. but JavaScript is a dynamic language. Hence the IDE’s assistance is very limited. You need to pick individual tools and build your tool suite.

Workflow activities like running tests, linting the source code for quality, transpile the source to ES5 version can be automated using task runners using Gulp. PHP’s Laravel framework too uses gulp.

For linting, you need to use eslint and jshint.

For break point debugging and live reload, you need to use a combination of node-inspector and nodemon and run them combined.

IDEs like WebStorm will have everything integrated into the environment out of the box.

Guides – indentation

Node.js uses 2 spaces. npm uses 2 spaces. Google JavaScript style guide uses 2 spaces. Felixge’s node-style-guide uses 2 spaces. Even Ryan Dahl, the creator of Node.js uses 2 space. So you’re better off using 2-Spaces for indentation.

Deploying

You can run the Node.js application in any port you want, but it is highly desirable to have a public url address without port number. Since the default HTTP port 80 falls under 1024, you could require a sudo access to run the application on this port. This may lead to conflict with the web servers like Apache or Nginx that require port 80 to run. We can avoid this by the starting the application in ports > 1024 using process manager like pm2 and then fronting it with Nginx.