HTML and CSS Reference
In-Depth Information
E.2. Easy web apps with Node modules
In the previous section, you explored several functions that need to be performed in Node
in order to create real web applications: placing dynamic content inside static files (or tem-
plating) and mapping requests at different URLs to appropriate handlers (or routing). But
you also need to handle WebSocket requests. That was the point of installing Node in the
first place. If you looked at the explanation of the WebSocket protocol in appendix D , you
know that handling WebSockets involves a lot of slicing and dicing of binary data. This is
hardly the use case for which JavaScript was designed. You don't want to spend all your
time dealing with low-level stuff like that when you could be writing applications. All of
this can be more easily accomplished in Node by taking advantage of third-party modules.
In this section we'll set you up with the following modules:
Director —for routing
Mustache —for templating
WebSocket-Node —for the WebSocket protocol
You can easily manage modules with the NPM script. Because modules have become fun-
damental to using Node, NPM, once a handy add-on, now comes as part of the main
Node.js distribution.
It's easy to install the modules; Node looks for them in the node_modules directory of the
current directory. This will be the same place where your app.js file is located, so make
sure you're in that directory before installing modules. Then, run these commands:
npm install director
npm install mustache
npm install websocket
Modules are now installed local to your application, and you have a file structure like that
shown in figure E.3 .
Search WWH ::




Custom Search