HTML and CSS Reference
In-Depth Information
as will Node's event module. Listing 14.3 shows the server, which should live in
lib/chapp/server.js .
Listing 14.3 A Node.js HTTP server
var http = require("http");
var url = require("url");
var crController = require("chapp/chat_room_controller");
module.exports = http.createServer(function (req, res) {
if (url.parse(req.url).pathname == "/comet") {
var controller = crController.create(req, res);
The server requires the first module that we are going to write—the chat-
RoomController , which deals with the request/response logic. The server cur-
rently only responds to requests to the /comet URL. The Startup Script
To start the server we need a script similar to the run _ tests script, which sets up
the load path, requires the server file, and starts the server. Listing 14.4 shows the
script, which should be saved in ./run _ server , and should be made executable
with chmod +x run _ server .
Listing 14.4 Startup script
require.paths.push( __ dirname);
require.paths.push( __ dirname + "/deps");
require.paths.push( __ dirname + "/lib");
The listen call starts the server. process.argv contains all the command
line arguments, i.e., the interpreter, the file being run, and any additional arguments
given when running the script. The script is run with ./run _ server 8080 .
Leaving out the port number starts the server on the default port 8000.
Search WWH ::

Custom Search