@ -16,8 +16,8 @@ An example of a web server written with Node which responds with "Hello
World":
World":
----------------------------------------
----------------------------------------
include("/utils.js" );
node.mixin(require("/utils.js") );
include("/http.js" );
node.mixin(require("/http.js") );
createServer(function (request, response) {
createServer(function (request, response) {
response.sendHeader(200, {"Content-Type": "text/plain"});
response.sendHeader(200, {"Content-Type": "text/plain"});
response.sendBody("Hello World\n");
response.sendBody("Hello World\n");
@ -61,11 +61,23 @@ error reporting.
The filename of the script being executed.
The filename of the script being executed.
+require(path)+ ::
+require(path)+ ::
+include(path)+ ::
See the modules section.
See the modules section.
+node.libraryPaths+ ::
+node.libraryPaths+ ::
The search path for absolute path arguments to +require()+ and +include()+.
The search path for absolute path arguments to +require()+.
+node.mixin([deep], target, object1, [objectN])+ ::
Extend one object with one or more others, returning the modified object.
If no target is specified, the +process+ namespace itself is extended.
Keep in mind that the target object will be modified, and will be returned
from +node.mixin()+.
+
If a boolean true is specified as the first argument, Node performs a deep
copy, recursively copying any objects it finds. Otherwise, the copy will
share structure with the original object(s).
+
Undefined properties are not copied. However, properties inherited from the
object's prototype will be copied over.
=== The +process+ Object
=== The +process+ Object
@ -119,7 +131,7 @@ Executes the command as a child process, buffers the output and returns it
in a promise callback.
in a promise callback.
+
+
----------------------------------------
----------------------------------------
include("/utils.js" );
node.mixin(require("/utils.js") );
exec("ls /").addCallback(function (stdout, stderr) {
exec("ls /").addCallback(function (stdout, stderr) {
puts(stdout);
puts(stdout);
});
});
@ -269,7 +281,7 @@ The contents of +foo.js+:
----------------------------------------
----------------------------------------
var circle = require("circle.js");
var circle = require("circle.js");
include("/utils.js" );
node.mixin(require("/utils.js") );
puts("The area of a circle of radius 4 is " + circle.area(4));
puts("The area of a circle of radius 4 is " + circle.area(4));
----------------------------------------
----------------------------------------
@ -292,24 +304,20 @@ The module +circle.js+ has exported the functions +area()+ and
object. (Alternatively, one can use +this+ instead of +exports+.) Variables
object. (Alternatively, one can use +this+ instead of +exports+.) Variables
local to the module will be private. In this example the variable +PI+ is
local to the module will be private. In this example the variable +PI+ is
private to +circle.js+. The function +puts()+ comes from the module
private to +circle.js+. The function +puts()+ comes from the module
+"/utils.js"+. Because +include("/utils.js")+ was called, +puts()+ is in the
+"/utils.js"+.
global namespace.
The module path is relative to the file calling +require()+. That is,
The module path is relative to the file calling +require()+. That is,
+circle.js+ must be in the same directory as +foo.js+ for +require()+ to
+circle.js+ must be in the same directory as +foo.js+ for +require()+ to
find it.
find it.
Like +require()+ the function +include()+ also loads a module. Instead of
Use +node.mixin()+ to include modules into the global namespace.
returning a namespace object, +include()+ will add the module's exports into
the global namespace. For example:
----------------------------------------
----------------------------------------
include("circle.js");
node.mixin(process, require("circle.js"), require("/utils.js"));
include("/utils.js");
puts("The area of a cirlce of radius 4 is " + area(4));
puts("The area of a cirlce of radius 4 is " + area(4));
----------------------------------------
----------------------------------------
When an absolute path is given to +require()+ or +include()+ , like
When an absolute path is given to +require()+, like
+require("/mjsunit.js")+ the module is searched for in the
+require("/mjsunit.js")+ the module is searched for in the
+node.libraryPaths+ array. +node.libraryPaths+ on my system looks like this:
+node.libraryPaths+ array. +node.libraryPaths+ on my system looks like this:
@ -567,8 +575,7 @@ Objects returned from +node.fs.stat()+ are of this type.
=== HTTP
=== HTTP
To use the HTTP server and client one must +require("/http.js")+ or
To use the HTTP server and client one must +require("/http.js")+.
+include("/http.js")+.
The HTTP interfaces in Node are designed to support many features
The HTTP interfaces in Node are designed to support many features
of the protocol which have been traditionally difficult to use.
of the protocol which have been traditionally difficult to use.
@ -990,8 +997,7 @@ stream.addListener('complete', function() {
=== TCP
=== TCP
To use the TCP server and client one must +require("/tcp.js")+ or
To use the TCP server and client one must +require("/tcp.js")+.
+include("/tcp.js")+.
==== +tcp.Server+
==== +tcp.Server+
@ -999,7 +1005,7 @@ Here is an example of a echo server which listens for connections
on port 7000
on port 7000
----------------------------------------
----------------------------------------
include("/tcp.js" );
node.mixin(require("/tcp.js") );
var server = createServer(function (socket) {
var server = createServer(function (socket) {
socket.setEncoding("utf8");
socket.setEncoding("utf8");
socket.addListener("connect", function () {
socket.addListener("connect", function () {
@ -1237,8 +1243,8 @@ result of the last expression.
The library is called +/repl.js+ and it can be used like this:
The library is called +/repl.js+ and it can be used like this:
------------------------------------
------------------------------------
include("/utils.js" );
node.mixin(require("/utils.js") );
include("/tcp.js" );
node.mixin(require("/tcp.js") );
nconnections = 0;
nconnections = 0;
createServer(function (c) {
createServer(function (c) {
error("Connection!");
error("Connection!");