A file is a module
Exports public interface
Create Exports.js
Two common interfaces available externally: Set value, square
var i;exports.set = function (num) {//Set value i=num; Console.log ("Seti to" +i); Exports.square = function () {//squared and output I=math.pow (i,2); Console.log ("i^2=" +i);
Require Get External interface
Create Require.js
var square =require ("./exports");//Load Module Square.set (5); Square.square ();
Run effect
After learning the module, we encapsulate the service request as a module backup.
Encapsulating the Boot module
New Index.js
var server = require ("./server"); Server.start ();
New Server.js
var http = require ("http"); function start () {function onrequest (request, Response) { Console.log ("request received."); Response.writehead ($, {"Content-type": "Text/plain"}); Response.Write ("Hello world!"); Response.End (); } Http.createserver (ONrequest). Listen (8000); Console.log ("Server has started."); Exports.start = start;
Test effect
Reference http://www.cnblogs.com/joqk/p/3868044.html
"Node. JS Learning"--(2)--module