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英文参考文献翻译
- 下一篇:学生信息管理系统英文文献和中文翻译
-
-
-
-
-
-
-
巴金《激流三部曲》高觉新的悲剧命运
上市公司股权结构对经营绩效的影响研究
现代简约美式风格在室内家装中的运用
高警觉工作人群的元情绪...
中国传统元素在游戏角色...
C++最短路径算法研究和程序设计
NFC协议物理层的软件实现+文献综述
浅析中国古代宗法制度
江苏省某高中学生体质现状的调查研究
g-C3N4光催化剂的制备和光催化性能研究