HTML and CSS Reference
In-Depth Information
var room = Object.create(chatRoom);
module.exports = http.createServer(function (req, res) {
if (url.parse(req.url).pathname == "/comet") {
var controller = crController.create(req, res);
controller.chatRoom = room;
controller[req.method.toLowerCase()]();
}
});
For this to work, we need to add a fake chatRoom module. Save the contents
of Listing 14.26 to lib/chapp/chat _ room.js .
Listing 14.26 A fake chat room
var sys = require("sys");
var chatRoom = {
addMessage: function (user, message) {
sys.puts(user + ": " + message);
}
};
module.exports = chatRoom;
Listing 14.27 shows how to use node-repl , an interactive Node shell, to
encode some POST data and post it to the application using curl , the command
line HTTP client. Run it in another shell, and watch the output from the shell that
is running the application.
Listing 14.27 Manually testing the app from the command line
$ node-repl
node> var msg = { user:"cjno", message:"Enjoying Node.js" };
node> var data = { topic: "message", data: msg };
node> var encoded = encodeURI(JSON.stringify(data));
node> require("fs").writeFileSync("chapp.txt", encoded);
node> Ctrl-d
$ curl -d `cat chapp.txt` http://localhost:8000/comet
When you enter that last command, you should get an immediate response (i.e.,
it simply returns to your prompt) and the shell that is running the server should
output “cjno: Enjoying Node.js.” In Chapter 15, TDD and DOMManipulation: The
Chat Client, we will build a proper frontend for the application.
 
Search WWH ::




Custom Search