![](https://csdnimg.cn/release/download_crawler_static/9222347/bg13.jpg)
For now, the important line in this source file is the first line that uses the Node.js
require() g
lobal function. The require() function makes a Node.js module available
for use. Node.js modules are what you might expect: a collection of data and functions
that are bundled together, usually providing functionality in some particular area. In
this case, the http Node.js module provides simple HTTP server functionality.
The node executable has a number of built-in modules: http, https, fs, path, crypto,
url, net, dgram, dns, tls, and child_process. Expect these built-in modules and their
functionality to vary from version to version.
By design, a module resides in a namespace. A namespace is an extra specification that
is added to the front of a data or function reference; for example, http is the namespace
that the createServer() function resides in. In Node.js, a namespace is just imple‐
mented as an object. When the http module is loaded, the require() function returns
an object and that object is assigned to the http variable. The variable does not have to
be called “http”; it could be called “xyzzybub” and, in that case, the server would be
created by calling the xyzzybub.createServer() function.
Why have a namespace? Why not just put all the data and functions as global variables?
Node.js anticipated that new modules with new functionality, such as a MySQL access,
would be developed by other people and need to be integrated into the node executable
after Node.js was already installed on a user’s computer. Since the names of data and
functions in those modules would be unpredictable, a developer might accidentally
choose the exact same name for a function in a module as a different developer might
choose for another module. But since a module is contained in a namespace, the name‐
space would distinguish between the two functions. In fact, an important improvement
over previous languages, such as C++ and Java, is that Node.js allows the user of the
module to specify the name of the namespace because the user himself assigns the
module to his variable, such as http or xyzzybub.
These new modules with new functionality are packages. A package is a module that
can be added to the node executable later and is not built into the node executable by
default. The difference between a module and a package is not very important; it is really
just a change of terminology.
The npm (node package manager) executable adds new packages to the node executable.
To install a package, first use Google or another search engine to find the npm package
that you want to install. Often, the package will be found on GitHub. An alternative to
using a search engine is to use the npm executable itself to find the package using the
search command.
Instead of the web server that always returns a “Hello world!” page, suppose we want to
create a web server that actually serves up static web pages from files on the hard disk.
To find a Node.js static file server module, a good search phrase to type into a search
The node and npm Executables | 3