Real-time Collaboration Author:Shane Hudson Throughout the book so far, all the code we have written has been client side, but although client side JavaScript is very powerful nowadays it is often necessary to have a server so that data can be stored online, to keep sensitive logic out of the client, or so that users can interact with each other online. You may be familiar with other server side39695
languages such as PHP or Ruby (possibly even Perl!), but one of the advantages of Node.js is that you write server side code in JavaScript so it should be easy to pick up because you have client side JS experience.
Making a Chatroom
    In this chapter we use Node.js with Socket.IO to create a real time chatroom web app that allows multiple users to talk to each other. I guide you through setting up Node.js on your local machine because most commonly used ervers (shared hosting) do not support Node.js, but do be aware that some services such as Heroku and Amazon Web Services do have free tiers.
   Installing Node.js Obviously to write anything using Node.js, you need to have it installed. There are  many ways to install it. You can use a package manager such as apt-get, yum, or Homebrew (depending on which operating system you are using) but these are not always up to date. On the Node.js website, www.nodejs.org, you can download the latest version executable binaries or build it from source code. Alternatively, you can use the Node Version Manager (nvm) from www.github.com/creationix/nvm, which is particularly useful if you are working with many codebases that are not all using the latest version. Node comes with a built-in package manager called npm, which handles dependencies using a file called package.json, so rather than installing Express and Socket.IO yourself, you just put it in the file. When you
are ready to install the packages, run npm install within the folder that has package.json. Unlike traditional JavaScript, Node.js has a module system that allows you to load JavaScript files at runtime. Each module is defined in a file, exporting variables and functions through a module.exports object. In another file, you can use the 'require' function to load the module and get to its module.exports contents. There are three different ways to load a module.
• require() a relative file, for example require('./lib/utils.js'). This requires the single file and returns the value of 'module.exports' in that file. You’ll use this method quite a lot if you have large, multi-file projects.
• require() a core module, which are special keywords, for example require('http') to load the core http module. Node.js comes installed with a host of standard core modules, but they are not included by default, meaning you have to require them when you want them. You can see the full list of core modules in the Node.js documentation (www.nodejs.org/api/index.html).
• require() a distinct module from a node_modules folder, for example require('express').This is the most powerful feature of the module system. You can require entire separate modules by referencing the module name. The way this works is that Node.js has special knowledge of the node_modules folder. It walks up the directory tree from the current script, looking for node_modules/<name>/package.json or node_modules/<name>/index.js. It looks at the package.json first, is to see whether it has a “main” property, which references a .js file to load that is named differently to index.js. Let’s say you have a file /www/myapp/index.js, in this file you have require('express'). Node.js looks for the module in the
following directories:
• /www/myapp/node_modules/express/(package.json or index.js)
• /www/node_modules/express/(package.json or index.js)
• /node_modules/express/(package.json or index.js)
• $NODE_PATH/express/(package.json or index.js)
• $HOME/.node_modules/express/(package.json or index.js)
上一篇:ASP英文参考文献翻译
下一篇:学生信息管理系统英文文献和中文翻译

AngularJS技术介绍英文文献和中文翻译

开关电源水冷却系统英文文献和中文翻译

减数分裂和基因重组英文文献和中文翻译

太阳能最大功率点追踪和...

移动码头的泊位分配问题英文文献和中文翻译

虚拟船舶装配集成建模方...

中学生科学探究中对等论...

AES算法GPU协处理下分组加...

酵母菌发酵生产天然香料...

压疮高危人群的标准化中...

基于Joomla平台的计算机学院网站设计与开发

浅论职工思想政治工作茬...

从政策角度谈黑龙江對俄...

上海居民的社会参与研究

提高教育质量,构建大學生...

STC89C52单片机NRF24L01的无线病房呼叫系统设计

浅谈高校行政管理人员的...