• $HOME/.node_libraries/express/(package.json or index.js)
• $PREFIX/lib/node/express/(package.json or index.js)
This is an ex • haustive list of the folders Node.js looks in; it is highly recommended that you ensure your modules are placed in the local node_modules folder, for speed and reliability. The other folders are mostly useful for when Node.js modules inside a node_modules folder require their own modules, so you can share modules between your project and your modules.
This is where npm steps in to make things even better. Due to the features of node_modules folders in Node.js, npm looks at the “dependencies” and “devDependencies” objects in your package.json, and download each to your node_modules folder. It also downloads all your modules’ dependencies, and your modules’ modules’ dependencies, and so on. The difference between “dependencies” and “devDependencies” is that npm will not install devDependencies of other modules, only dependencies. By default npm installs everything into the local node_modules folder. This isn’t always what you want, so if you install modules with npm install -g <module>, then it installs them globally (typically in $HOME/.node_modules/). This is really useful as you can use this to install command line programs written in Node.js. For example I use nodemon by Remy Sharp as an alternative way to run the node server while I am developing a project, because the default node [filename] approach to running a server does not watch for files that have been updated while the server is running. To install nodemon you run npm –g install nodemon, then run nodemon [filename] to run a server through nodemon. More information about nodemon can be found at www.remy.github.io/nodemon/
The simplest example of Node.js is, of course, Hello World. To serve a web page we need to require a module called http, which lets us handle the low-level aspects of HTTP. This module is a core module, so we use the first definition in the list. Once HTTP is required, we store it within a variable called http. We then use a method on http called createServer; this has a callback that lets you send a response (also known as the web page). To create the web page that gets served, we start by writing the headers using response.writeHead. The first parameter that needs to be provided is the HTTP status code 200 (OK). The second is the actual header, in this example we are only writing “Hello World” so the Content-Type can be text/plain, you may choose to use text/html if you want to write some HTML to the page. To write the actual content, you just use response.write. Once you’ve finished writing the response, end it with response.end(). Now you have a server but to actually serve the page you need to tell it to listen for activity on a port (in this case, port 8080).
Now that you know how to require a module and do something with it, let’s make our own simple module that makes use of the module.exports that I mentioned. This module will wrap up our Hello World code into a file called helloworld.js, which we then require as a variable called hello in server.js and call the server method.
Of course, this is an incredibly simplistic example but it shows you quite nicely how you can require a custom module and call a method based on the name that it is exported as rather than the name of the function. It may help to point out here the differences between a method and a function, because many people use them interchangeably (which is fine, but I prefer to be more specific). A method is used on an object, while a function is independent of any object.
Socket.IO and Express
I mentioned Socket.IO earlier, it was one of the packages we installed from package.json, but I did not explain what it does. Basically it is a wrapper for web sockets so that all browsers can support real-time communication. Socket.IO aims to make real-time apps possible in every browser and mobile device, blurring the differences between the different transport mechanisms. It’s care-free real-time 100% in JavaScript.
- 上一篇:ASP英文参考文献翻译
- 下一篇:学生信息管理系统英文文献和中文翻译
-
-
-
-
-
-
-
巴金《激流三部曲》高觉新的悲剧命运
上市公司股权结构对经营绩效的影响研究
现代简约美式风格在室内家装中的运用
高警觉工作人群的元情绪...
中国传统元素在游戏角色...
C++最短路径算法研究和程序设计
NFC协议物理层的软件实现+文献综述
浅析中国古代宗法制度
江苏省某高中学生体质现状的调查研究
g-C3N4光催化剂的制备和光催化性能研究