菜单
  
    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)
  1. 上一篇:ASP英文参考文献翻译
  2. 下一篇:学生信息管理系统英文文献和中文翻译
  1. 汽车乘员舱的声振耦合英文文献和中文翻译

  2. 立体光照成型的注塑模具...

  3. 数控机床英文文献和中文翻译

  4. 工业机械手英文文献和中文翻译

  5. 低频振动的铁路车轴的状...

  6. 接头的形状对沥青塞接头...

  7. 数控加工技术英文文献和中文翻译

  8. 当代大学生慈善意识研究+文献综述

  9. java+mysql车辆管理系统的设计+源代码

  10. 十二层带中心支撑钢结构...

  11. 电站锅炉暖风器设计任务书

  12. 河岸冲刷和泥沙淤积的监测国内外研究现状

  13. 杂拟谷盗体内共生菌沃尔...

  14. 酸性水汽提装置总汽提塔设计+CAD图纸

  15. 中考体育项目与体育教学合理结合的研究

  16. 乳业同业并购式全产业链...

  17. 大众媒体对公共政策制定的影响

  

About

751论文网手机版...

主页:http://www.751com.cn

关闭返回