一个单独的文件就是一个模块。加载模块使用require方法,该方法读取一个文件并执行,最后返回文件内部的exports对象。html
console.log("evaluating example.js"); var invisible = function () { console.log("invisible"); } exports.message = "hi"; exports.say = function () { console.log(message); }
使用require方法,加载example.js。node
var example = require('./example.js');
这时,变量example就对应模块中的exports对象,因而就能够经过这个变量,使用模块提供的各个方法。git
{ message: "hi", say: [Function] }
require方法默认读取js文件,因此能够省略js后缀名。github
var example = require('./example');
js文件名前面须要加上路径,能够是相对路径(相对于使用require方法的文件),也能够是绝对路径。若是省略路径,node.js会认为,你要加载一个核心模块,或者已经安装在本地 node_modules 目录中的模块。若是加载的是一个目录,node.js会首先寻找该目录中的 package.json 文件,加载该文件 main 属性提到的模块,不然就寻找该目录下的 index.js 文件。web
下面的例子是使用一行语句,定义一个最简单的模块。express
// addition.js exports.do = function(a, b){ return a + b };
上面的语句定义了一个加法模块,作法就是在exports对象上定义一个do方法,那就是供外部调用的方法。使用的时候,只要用require函数调用便可。npm
var add = require('./addition'); add.do(1,2) // 3
再看一个复杂一点的例子。编程
// foobar.js function foobar(){ this.foo = function(){ console.log('Hello foo'); } this.bar = function(){ console.log('Hello bar'); } } exports.foobar = foobar;
调用该模块的方法以下:json
var foobar = require('./foobar').foobar, test = new foobar(); test.bar(); // 'Hello bar'
有时,不须要exports返回一个对象,只须要它返回一个函数。这时,就要写成module.exports。数组
module.exports = function () { console.log("hello world") }
比PHP性能要高不少
node helloworld.js:执行js脚本
node 的 REPL 模式(同Python的Shell交互模式)
//app.js var http = require('http'); http.createServer(function(req, res) { res.writeHead(200, {'Content-Type': 'text/html'}); res.write(' <h1>Node.js</h1> '); res.end(' <p>Hello World</p> '); }).listen(3000); console.log("HTTP server is listening at port 3000.");
安装
$ npm install -g supervisor
使用
$ supervisor app.js
做用:开发时修改代码后会自动终止脚本,而后从新启动,方便调试。
例子:
//module.js var name; exports.setName = function(thyName) { name = thyName; }; exports.sayHello = function() { console.log('Hello ' + name); };
在同一目录下建立 getmodule.js,内容是:
//getmodule.js var myModule = require('./module'); myModule.setName('BYVoid'); myModule.sayHello();
运行node getmodule.js,结果是:
Hello BYVoid
这点和对象不同,不管调用多少次 require,得到的模块都是同一个
//loadmodule.js var hello1 = require('./module'); hello1.setName('BYVoid'); var hello2 = require('./module'); hello2.setName('BYVoid 2'); hello1.sayHello();
输出结果:
Hello BYVoid 2
结果被后者覆盖了。
//singleobject.js function Hello() { var name; this.setName = function (thyName) { name = thyName; }; this.sayHello = function () { console.log('Hello ' + name); }; }; exports.Hello = Hello; //简化后:module.exports = Hello;
简化前:
require('./singleobject').Hello
简化后能够直接获取对象:
//gethello.js var Hello = require('./hello'); hello = new Hello(); hello.setName('BYVoid'); hello.sayHello();
exports本是就是一个空对象,本质上是经过闭包来创建一个有限的的访问接口。(很简单却想了好久~)
commonJS规范以下:
Node.js 在调用某个包时,会首先检查包中 package.json 文件的 main 字段,将其做为包的接口模块,若是 package.json 或
main 字段不存在,会尝试寻找 index.js 或 index.node 做为包的接口。
例子:
{ "name": "mypackage", "description": "Sample package for CommonJS. This package demonstrates the required elements of a CommonJS package.", "version": "0.7.0", "keywords": [ "package", "example" ], "maintainers": [ { "name": "Bill Smith", "email": "bills@example.com", } ], "contributors": [ { "name": "BYVoid", "web": "http://www.byvoid.com/" } ], "bugs": { "mail": "dev@example.com", "web": "http://www.example.com/bugs" }, "licenses": [ { "type": "GPLv2", "url": "http://www.example.org/licenses/gpl.html" } ], "repositories": [ { "type": "git", "url": "http://github.com/BYVoid/mypackage.git" } ], "dependencies": { "webkit": "1.2", "ssl": { "gnutls": ["1.0", "2.0"], "openssl": "0.9.8" } } }
npm:
npm [install/i] [package_name]
全局模式:减小重复副本
npm [install/i] -g [package_name]
建立全局连接:把全局包当成本地包来用
$ npm link express
./node_modules/express -> /usr/local/lib/node_modules/express
npm install -g node-inspector //安装insepctor node --debug-brk=5858 debug.js //连接服务器 $ node-inspector//启动 http://127.0.0.1:8080/debug?port=5858 //浏览器中打开!