Node.js FUNdamentals: JavaScript on The Server - JavaScript and Node FUNdamentals: A Collection of Essential Basics (2014)

JavaScript and Node FUNdamentals: A Collection of Essential Basics (2014)

4. Node.js FUNdamentals: JavaScript on The Server

Node.js is a highly efficient and scalable non-blocking I/O platform that was build on top of Google Chrome V8 engine and its ECMAScript. This means that most front-end JavaScript (another implementation of ECMAScript) objects, functions and methods are available in Node.js. Please refer to JavaScript FUNdamentals if you need a refresher on JS-specific basics.

Developers can install Node.js from its website and follow this overview of main Node.js concepts. For more meticulous Node.js instructions, take a look at Rapid Prototyping with JS: Agile JavaScript Development and Node School.

4.1 Read-Eval-Print Loop (a.k.a. Console) in Node.js

Like in many other programming language and platforms, Node.js has a read-eval-print loop tool which is opened by $ node command. The prompt changes to > and we can execute JavaScript akin to Chrome Developer Tools console. There are slight deviations in ECMAScript implementations in Node.js and browsers (e.g., {}+{}), but for the most part the results are similar.

So as you see, we can write JavaScript in the console all day long, but sometime we can to save script so we can run them later.

4.2 Launching Node.js Scripts

To start a Node.js script from a file, simply run $ node filename, e.g., $ node program.js. If all we need is a quick set of statements, there’s a -e option that allow to run inline JavaScript/Node.js, e.g., $ node -e "console.log(new Date());".

4.3 Node.js Process Information

Each Node.js script that is running is a process in its essence. For example, ps aux | grep 'node' will output all Node.js programs running on a machine. Conveniently, developers can access useful process information in code with process object, e.g., node -e "console.log(":

Node.js process examples using pid and cwd.

Node.js process examples using pid and cwd.

4.4 Accessing Global Scope in Node.js

As you know from JS FUNdamentals, browser JavaScript by default puts everything into its global scope. This was coined as one of the bad part of JavaScript in Douglas Crockford’s famous [JavaScript: The Good Parts]. Node.js was designed to behave differently with everything being local by default. In case we need to access globals, there is a global object. Likewise, when we need to export something, we should do so explicitly.

In a sense, window object from front-end/browser JavaScript metamorphosed into a combination of global and process objects. Needless to say, the document object that represent DOM of the webpage is nonexistent in Node.js.

4.5 Exporting and Importing Modules

Another bad part in browser JavaScript is that there’s no way to include modules. Scripts are supposed to be linked together using a different language (HTML) with a lacking dependency management. CommonJS and RequireJS solve this problem with AJAX-y approach. Node.js borrowed many things from the CommonJS concept.

To export an object in Node.js, use = object;, e.g.,

1 var messages = {

2 find: function(req, res, next) {

3 ...

4 },

5 add: function(req, res, next) {

6 ...

7 },

8 format: 'title | date | author'

9 }

10 exports.messages = messages;

While in the file where we import aforementioned script (assuming the path and the file name is route/messages.js):

1 var messages = require('./routes/messages.js');

However, sometime it’s more fitting to invoke a constructor, e.g., when we attach properties to Express.js app (more on Express.js in Express.js FUNdamentals: An Essential Overview of Express.js). In this case module.exports is needed:

1 module.exports = function(app) {

2 app.set('port', process.env.PORT || 3000);

3 app.set('views', __dirname + '/views');

4 app.set('view engine', 'jade');

5 return app;

6 }

In the file that includes the example module above:

1 ...

2 var app = express();

3 var config = require('./config/index.js');

4 app = config(app);

5 ...

The more succinct code: var = express(); require('./config/index.js')(app);.

The most common mistake when including modules is a wrong path to the file. For core Node.js modules, just use the name without any path, e.g., require(‘name’). Same goes for modules in node_modules folder. More on that later in the NPM section.

For all other files, use . with or without a file extension, e.g.,

1 var keys = require('./keys.js'),

2 messages = require('./routes/messages.js');

In addition, for the latter category it’s possible to use a longer looking statements with __dirname and path.join(), e.g., require(path.join(__dirname, ,’routes’, ‘messages’));`

If require() points to a folder, Node.js will attempt to read index.js file in that folder.

4.6 Buffer is a Node.js Super Data Type

Buffer is a Node.js addition to four primitives (boolean, string, number and RegExp) and all-encompassing objects (array and functions are also objects) in front-end JavaScript. We can think of buffers as extremely efficient data stores. In fact, Node.js will try to use buffers any time it can, e.g., reading from file system, receiving packets over the network.

4.7 __dirname vs. process.cwd

__dirname is an absolute path to the file in which this global variable was called, while process.cwd is an absolute path to the process that runs this script. The latter might not be the same as the former if we started the program from a different folder, e.g., $ node ./code/program.js.

4.8 Handy Utilities in Node.js

Although, the core of the Node.js platform was intentionally kept small it has some essential utilities such as


· Crypto

· Path

· String Decoder

The method that we use in this tutorials is path.join and it concatenates path using an appropriate folder separator (/ or \\).

4.9 Reading and Writing from/to The File System in Node.js

Reading from files is done via the core fs module. There are two sets of methods: async and sync. In most cases developers should use async methods, e.g., fs.readFile:

1 var fs = require('fs');

2 var path = require('path');

3 fs.readFile(path.join(__dirname, '/data/customers.csv'), {encoding: 'utf-8'}, fun\

4 ction (err, data) {

5 if (err) throw err;

6 console.log(data);

7 });

And the writing to the file:

1 var fs = require('fs');

2 fs.writeFile('message.txt', 'Hello World!', function (err) {

3 if (err) throw err;

4 console.log('Writing is done.');

5 });

4.10 Streaming Data in Node.js

Streaming data is a term that mean an application processes the data while it’s still receiving it. This is useful for extra large datasets, like video or database migrations.

Here’s a basic example on using streams that output the binary file content back:

1 var fs = require('fs');

2 fs.createReadStream('./data/customers.csv').pipe(process.stdout);

By default, Node.js uses buffers for streams.

For a more immersive training, take a loot at stream-adventure and Stream Handbook.

4.11 Installing Node.js Modules with NPM

NPM comes with the Node.js platform and allows for seamless Node.js package management. The way npm install work is similar to Git in a way how it traverses the working tree to find a current project. For starter, keep in mind that we need either the package.json file or thenode_modules folder, in order to install modules locally with $ npm install name, for example $ npm install superagent; in the program.js: var suparagent = requier('superagent');.

The best thing about NPM is that it keep all the dependencies local, so if module A uses module B v1.3 and module C uses module B v2.0 (with breaking changes comparing to v1.3), both A and C will have their own localized copies of different versions of B. This proves to be a more superior strategy unlike Ruby and other platforms that use global installations by default.

The best practice is not to include a node_modules folder into Git repository when the project is a module that supposed to be use in other application. However, it’s recommended to include node_modules for deployable applications. This prevents a breakage caused by unfortunate dependency update.

Note: The NPM creator like to call it npm (lowercase).

4.12 Hello World Server with HTTP Node.js Module

Although, Node.js can be use for a wide variety of tasks, it’s mostly knows for building web applications. Node.js is thrives in the network due to its asynchronous nature and build-in modules such as net and http.

Here’s a quintessential Hello World examples where we create a server object, define request handler (function with req and res arguments), pass some data back to the recipient and start up the whole thing.

1 var http = require('http');

2 http.createServer(function (req, res) {

3 res.writeHead(200, {'Content-Type': 'text/plain'});

4 res.end('Hello World\n');

5 }).listen(1337, '');

6 console.log('Server running at');]

The req and res parameters have all the information about a given HTTP request and response correspondingly. In addition, req and res can be used as streams (look in the previous section).

4.13 Debugging Node.js Programs

The best debugger is console.log(), but sometime we need to see the call stack and orient ourselves in async code a bit more. To do that, put debugger statements in your code and use $ node debug program.js to start the debugging process. For more developer-friendly interface, downloadnode inspector.

4.14 Taming Callbacks in Node.js

Callbacks are able to Node.js code asynchronous, yet programmers unfamiliar with JavaScript, who come from Java or PHP, might be surprised when they see Node.js code described on Callback Hell:

1 fs.readdir(source, function(err, files) {

2 if (err) {

3 console.log('Error finding files: ' + err)

4 } else {

5 files.forEach(function(filename, fileIndex) {

6 console.log(filename)

7 gm(source + filename).size(function(err, values) {

8 if (err) {

9 console.log('Error identifying file size: ' + err)

10 } else {

11 console.log(filename + ' : ' + values)

12 aspect = (values.width / values.height)

13 widths.forEach(function(width, widthIndex) {

14 height = Math.round(width / aspect)

15 console.log('resizing ' + filename + 'to ' + height + 'x' + height)

16 this.resize(width, height).write(destination + 'w' + width + '_' + fi\

17 lename, function(err) {

18 if (err) console.log('Error writing file: ' + err)

19 })

20 }.bind(this))

21 }

22 })

23 })

24 }

25 })

There’s nothing to be afraid of here as long as two-space indentation is used. ;-) However, callback code can be re-write with the use of event emitters, promises or by utilizing the async library.

4.15 Introduction to Node.js with Ryan Dahl

Last, but not least, take a few minutes and watch this Introduction to Node.js with Ryan Dahl video.

4.16 Moving Forward with Express.js

After you’ve mastered Node.js basics in this article, you might want to read Express.js FUNdamentals: An Essential Overview of Express.js and consider working on an interactive class about the Express.js framework which is as of today is the most popular module on NPM.