首先EditPlus編輯器,打開新建的文本文檔,另存爲副本html
調用函數分爲調用本地函數,和其餘文件的函數編輯器
一、調用本地函數函數
var http = require('http'); http.createServer(function (request, response) { response.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'}); if(request.url!=="/favicon.ico"){ //清除第2此訪問 fun1(response); //調用函數 response.end(''); } }).listen(8000); console.log('Server running at http://127.0.0.1:8888/'); //本地函數 function fun1(res){ //res形參 是給客戶端傳輸響應數據的response console.log("fun1"); res.write("hello,我是fun1"); }
二、調用其餘函數ui
經過上面新建的文本文檔 -再次另存爲副本 創建n2_otherfuncall.js文件url
n2_funcall.js 中引用n2_otherfuncall.js文件spa
var http = require('http'); var other = require('./n2_otherfuncall.js'); http.createServer(function (request, response) { response.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'}); if(request.url!=="/favicon.ico"){ //清除第2此訪問 //fun1(response); //調用函數 other(response);//調用其餘函數 //調用多個函數的寫法 other.fun2(response); other.fun3(response); //或者 other['fun2'](response); other['fun3'](response); response.end(''); } }).listen(8888); console.log('Server running at http://127.0.0.1:8888/');
n2_otherfuncall.js文件code
function fun2(res){ console.log("fun2"); res.write("你好,我是fun2"); } //module.exports 對象是由模塊系統建立的。 //在咱們本身寫模塊的時候, //須要在模塊最後寫好模塊接口, //聲明這個模塊對外暴露什麼內容, //module.exports 提供了暴露接口的方法 module.exports = fun2;//module.exports只支持一個函數 //下面是支持多個函數的寫法 module.exports = { fun2:function(res){ console.log("我是fun2"); res.write("你好,我是fun2"); }, fun3:function(res){ console.log("我是fun3"); res.write("你好,我是fun3"); } }