15
Introduction to Node.js

Node.js is an open-source project that lets you write JavaScript that runs outside the browser.

When you write JavaScript for the browser, your code is given access to global objects like the document and window, as well as other APIs and libraries. With Node, your code can access the hard drive, databases, and the network (Figure 15.1).

Figure 15.1  JavaScript running in browser vs via Node

JavaScript running in browser vs via Node

Using Node, you can create anything from command-line tools to web servers. Over the next four chapters, you will use Node to help create a real-time chat application called Chattrbox (Figure 15.2).

Figure 15.2  Chattrbox: strictly for important conversations

Chattrbox: strictly for important conversations

Chattrbox will consist of two parts: a Node.js server and a JavaScript app running in the browser. The browser will connect to the Node server and receive the HTML, CSS, and JavaScript files. At that point, the JavaScript app in the browser will begin handling real-time communication over WebSockets. This process is diagrammed in Figure 15.3.

Figure 15.3  Network diagram of the Chattrbox application

Network diagram of the Chattrbox application

You will learn about WebSockets in the next chapter. This chapter focuses on getting you familiar with Node.

Node and npm

When you installed Node.js in Chapter 1, you got access to two command-line programs: node and the Node package manager, npm. You may recall that npm allows you to install open-source development tools, like browser-sync. The node program does the work of running programs written in JavaScript.

Most of your work will be with npm in this chapter. The npm command-line tool can perform a variety of tasks, like installing third-party code that you can incorporate into your project and managing your project’s workflow and external dependencies. In this chapter, you will be using npm to:

  • create the package.json file, using npm init

  • add third-party modules, using npm install --save

  • run frequently used tools saved in package.json’s scripts section

Node is much more than the node and npm commands. It also includes a number of useful modules that provide constructors to help you do things like work with files and folders, communicate over a network, and handle events. Also, when writing JavaScript for Node you will have access to utility functions that facilitate JavaScript’s interaction with the Node module ecosystem. For example, Node provides a much simpler module pattern than the IIFEs you used for CoffeeRun.

The package.json file mentioned above is a file that acts as your Node project’s manifest. It holds your project’s name, version number, description, and other information. More important, it is where you can store configuration settings and commands for npm to use when testing and building your application.

You could create this file by hand, but it is much easier to let npm do it for you.

npm init

Create a directory in your projects folder named chattrbox. In your terminal program, change to that directory and run npm init to have npm create package.json.

npm will prompt you for information about the project. It will also offer default answers, which are fine for now. Press the Return key to accept the defaults (Figure 15.4).

Figure 15.4  Running npm init

Running npm init

Open the chattrbox project folder in Atom. You will see that the package.json file was indeed created for you (Figure 15.5).

Figure 15.5  package.json contents after npm init

package.json contents after npm init

npm scripts

In package.json, notice the section labeled "scripts". This is for commands that you might need to run again and again while working on your project.

As you build Chattrbox, you will add to the "scripts" section of package.json to make your development workflow more efficient. Create your first npm workflow script by adding a "start" script (note that you must add a comma to the end of the "test" line):

...
  "scripts": {
    "test": "echo "Error: no test specified" && exit 1",
    "start": "node index.js"
  },
...

This lets you start your Node server by running npm start from the command line.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.122.11