Function call 1. Common function calls within a file
Create a JS file named 2_callfunction.js, which defines a function fun1, outputting a string to the returned object "Hello, I am fun1".
1 //--------------------2_callfunction.js---------------------------------2 varHTTP = require (' http '); 3Http.createserver (function(Request, response) {4Response.writehead, {' Content-type ': ' text/html; Charset=utf-8 '}); 5if(request.url!== "/favicon.ico") {//Clear 2nd this access6FUN1 (response);//call normal function fun17 Response.End (); 8 } 9}). Listen (8000); TenConsole.log (' Server running at Http://127.0.0.1:8000/'); One //---Common functions A functionfun1 (res) { -Res.write ("Hello, I am fun1"); -}
We run: node 2_callfunction, open Browser
2. Calling functions in other files
First we create a otherfuns.js file, there are two functions in the file, the call function is called by the Controller, both of which output a string to the response object, the function is provided to the external call through Module.exports. Here we only provide a function controller for the external
1 //-------------------models/otherfuns.js--------------------------2 functionController (RES) {3Res.write ("Executive controller <br>"); 4 Call (res); 5 Res.end (); 6 } 7 functionCall (res) {8Res.write ("Execute Call"); 9 } TenModule.exports = Controller;//only one function is supported
We introduce otherfuns.js into the main file through require, require working mechanism see require () Source code interpretation.
The following two sentences can be called to the controller function:
1) var other =new otherfun (response); The method controller in//otherfuns.js is called
2) Otherfun (response); The method controller in//otherfuns.js is called
1 //--------------------2_callfunction.js---------------------------------call the function in Otherfuns.js2 varHTTP = require (' http '); 3 varOtherfun = require ('./models/otherfuns.js ')); 4Http.createserver (function(Request, response) {5Response.writehead, {' Content-type ': ' text/html; Charset=utf-8 '}); 6if(request.url!== "/favicon.ico") {//Clear 2nd this access7 varother =NewOtherfun (response);//the method in the Otherfuns.js controller is called8 //Otherfun (response); The method controller in//otherfuns.js is called9 Response.End (); Ten } One}). Listen (8000); AConsole.log (' Server running at Http://127.0.0.1:8000/');
We run: node 2_callfunction, open Browser
3. Provide a notation for invoking multiple functions:
The first type:
1 //supports multiple functions2 functionController (RES) {3Res.write ("Executive controller <br>"); 4 Res.end (); 5 } 6 functionCall (res) {7Res.write ("Execute Call"); 8 Res.end (); 9 } TenModule.exports.controller =Controller; OneModule.exports.call =call;
The second type:
1 //supports multiple functions2module.exports={ 3Controllerfunction(res) {4Res.write ("Executive controller <br>"); 5 Res.end (); 6 }, 7Callfunction(res) {8Res.write ("Executive Call <br>"); 9 Res.end (); Ten } One}
The method of invocation, which is only supported by a function, needs to be modified to call the following method
Otherfun.controller (response); The function controller in//otherfuns.js is called
4. Modular Call Application (object-oriented)
We create a user object
1 //--------------user.js--------------2 functionUser (id,name,age) {3 This. Id=id;//Properties4 This. name=name;//Properties5 This. age=age;//Properties6 This. enter=function(){//Method7Console.log ("Access to Library");8 }9 }TenModule.exports = User;
Build a Teacher object again
1 //-------------------models/teacher.js---------2 varUser = require ('./user '));3 functionTeacher (id,name,age) {4User.apply ( This, [Id,name,age]);//Inherit user5 This. teach=function(RES) {//own Method6Res.write ( This. name+ "Teacher Lectures");7 }8 }9Module.exports = Teacher;
Teacher inherits the user object, with the Id,name,age property, and the Teach method is defined in addition to the Enter method.
Apply can be executed more than once, so you can inherit multiple objects, which is not as restrictive as other languages ' object orientation.
On the server side, teacher can be called as follows. Teacher (1, ' John Doe ', 30), initializes an instance of the object
1 //----------------------n3_modalcall.js-------------2 varHTTP = require (' http '); 3 varTeacher = require ('./models/teacher '));4Http.createserver (function(Request, response) {5Response.writehead, {' Content-type ': ' text/html; Charset=utf-8 '}); 6if(request.url!== "/favicon.ico") {//Clear 2nd this access7Teacher =NewTeacher (1, ' John Doe ', 30);8 Teacher.teach (response);9Response.End (' '); Ten } One}). Listen (8000); AConsole.log (' Server running at Http://127.0.0.1:8000/');
We run: node 3_modelcall, open Browser
Nodejs Advanced 2--Function Module call