|
@ -16,14 +16,14 @@ An example of a web server written with Node which responds with "Hello |
|
|
World": |
|
|
World": |
|
|
|
|
|
|
|
|
---------------------------------------- |
|
|
---------------------------------------- |
|
|
node.mixin(require("/utils.js")); |
|
|
var sys = require("/sys.js"), |
|
|
node.mixin(require("/http.js")); |
|
|
http = require("/http.js"); |
|
|
createServer(function (request, response) { |
|
|
http.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"); |
|
|
response.finish(); |
|
|
response.finish(); |
|
|
}).listen(8000); |
|
|
}).listen(8000); |
|
|
puts("Server running at http://127.0.0.1:8000/"); |
|
|
sys.puts("Server running at http://127.0.0.1:8000/"); |
|
|
---------------------------------------- |
|
|
---------------------------------------- |
|
|
|
|
|
|
|
|
To run the server, put the code into a file called +example.js+ and execute |
|
|
To run the server, put the code into a file called +example.js+ and execute |
|
@ -114,9 +114,9 @@ An array containing the command line arguments. |
|
|
An object containing the user environment. See environ(7). |
|
|
An object containing the user environment. See environ(7). |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== Utilities |
|
|
=== System module |
|
|
|
|
|
|
|
|
These function are in +"/utils.js"+. Use +require("/utils.js")+ to access them. |
|
|
These function are in +"/sys.js"+. Use +require("/sys.js")+ to access them. |
|
|
|
|
|
|
|
|
+puts(string)+:: |
|
|
+puts(string)+:: |
|
|
Outputs the +string+ and a trailing new-line to +stdout+. |
|
|
Outputs the +string+ and a trailing new-line to +stdout+. |
|
@ -136,8 +136,8 @@ Executes the command as a child process, buffers the output and returns it |
|
|
in a promise callback. |
|
|
in a promise callback. |
|
|
+ |
|
|
+ |
|
|
---------------------------------------- |
|
|
---------------------------------------- |
|
|
node.mixin(require("/utils.js")); |
|
|
var sys = require("/sys.js"); |
|
|
exec("ls /").addCallback(function (stdout, stderr) { |
|
|
sys.exec("ls /").addCallback(function (stdout, stderr) { |
|
|
puts(stdout); |
|
|
puts(stdout); |
|
|
}); |
|
|
}); |
|
|
---------------------------------------- |
|
|
---------------------------------------- |
|
@ -285,9 +285,9 @@ one-to-one correspondence. As an example, +foo.js+ loads the module |
|
|
The contents of +foo.js+: |
|
|
The contents of +foo.js+: |
|
|
|
|
|
|
|
|
---------------------------------------- |
|
|
---------------------------------------- |
|
|
var circle = require("circle.js"); |
|
|
var circle = require("circle.js"), |
|
|
node.mixin(require("/utils.js")); |
|
|
sys = require("/sys.js"); |
|
|
puts("The area of a circle of radius 4 is " + circle.area(4)); |
|
|
sys.puts("The area of a circle of radius 4 is " + circle.area(4)); |
|
|
---------------------------------------- |
|
|
---------------------------------------- |
|
|
|
|
|
|
|
|
The contents of +circle.js+: |
|
|
The contents of +circle.js+: |
|
@ -309,7 +309,7 @@ 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"+. |
|
|
+"/sys.js"+. |
|
|
|
|
|
|
|
|
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 |
|
@ -318,7 +318,7 @@ find it. |
|
|
Use +node.mixin()+ to include modules into the global namespace. |
|
|
Use +node.mixin()+ to include modules into the global namespace. |
|
|
|
|
|
|
|
|
---------------------------------------- |
|
|
---------------------------------------- |
|
|
node.mixin(process, require("circle.js"), require("/utils.js")); |
|
|
node.mixin(process, require("circle.js"), require("/sys.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)); |
|
|
---------------------------------------- |
|
|
---------------------------------------- |
|
|
|
|
|
|
|
@ -410,7 +410,7 @@ ls.addListener("output", function (data) { |
|
|
---------------------------------------- |
|
|
---------------------------------------- |
|
|
+ |
|
|
+ |
|
|
Note, if you just want to buffer the output of a command and return it, then |
|
|
Note, if you just want to buffer the output of a command and return it, then |
|
|
+exec()+ in +/utils.js+ might be better. |
|
|
+exec()+ in +/sys.js+ might be better. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+child.pid+ :: |
|
|
+child.pid+ :: |
|
@ -1014,8 +1014,8 @@ Here is an example of a echo server which listens for connections |
|
|
on port 7000 |
|
|
on port 7000 |
|
|
|
|
|
|
|
|
---------------------------------------- |
|
|
---------------------------------------- |
|
|
node.mixin(require("/tcp.js")); |
|
|
var tcp = require("/tcp.js"); |
|
|
var server = createServer(function (socket) { |
|
|
var server = tcp.createServer(function (socket) { |
|
|
socket.setEncoding("utf8"); |
|
|
socket.setEncoding("utf8"); |
|
|
socket.addListener("connect", function () { |
|
|
socket.addListener("connect", function () { |
|
|
socket.send("hello\r\n"); |
|
|
socket.send("hello\r\n"); |
|
@ -1252,15 +1252,16 @@ 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: |
|
|
------------------------------------ |
|
|
------------------------------------ |
|
|
node.mixin(require("/utils.js")); |
|
|
var sys = require("/sys.js"), |
|
|
node.mixin(require("/tcp.js")); |
|
|
tcp = require("/tcp.js"), |
|
|
|
|
|
repl = require("/repl.js"); |
|
|
nconnections = 0; |
|
|
nconnections = 0; |
|
|
createServer(function (c) { |
|
|
tcp.createServer(function (c) { |
|
|
error("Connection!"); |
|
|
sys.error("Connection!"); |
|
|
nconnections += 1; |
|
|
nconnections += 1; |
|
|
c.close(); |
|
|
c.close(); |
|
|
}).listen(5000); |
|
|
}).listen(5000); |
|
|
require("/repl.js").start("simple tcp server> "); |
|
|
repl.start("simple tcp server> "); |
|
|
------------------------------------ |
|
|
------------------------------------ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|