nodejs中函數(shù)的調(diào)用實例詳解
更新時間:2018年10月31日 10:02:54 作者:這天真熱呀
本文通過實例代碼給大家介紹了nodejs函數(shù)的調(diào)用,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
一、調(diào)用本js文件中的函數(shù)
var http = require('http');
http.createServer(function (request,response){
response.writeHead(200, {'Contet-Type':'text/html;charset=utf-8'});
if(request.url!=='/favicon.ico'){
funl(response);
response.end('');
}
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');
function funl(res){
console.log('fun1');
res.write('hello ,我是fun1');
}
運行結(jié)果:


二、調(diào)用外部的js文件


function fun2(res){
console.log('我是,fun2');
res.write('你好我是fun2');
}
// 想把此js聲明為一個函數(shù),加下面代碼,只適用于文件中只有一個函數(shù)
module.exports = fun2;
var http = require('http');
// ortherFun 就代替了fun2
var ortherFun = require('./../otherjs/out.js');
http.createServer(function (request,response){
response.writeHead(200, {'Contet-Type':'text/html;charset=utf-8'});
if(request.url!=='/favicon.ico'){
// funl(response);
ortherFun(response);
response.end('');
}
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');
function funl(res){
console.log('fun1');
res.write('hello ,我是fun1');
}


外部js文件內(nèi)有多個函數(shù)
// 支持多個函數(shù)
module.exports={
fun2:function(res){
console.log('我是fun2');
res.write('你好,我是fun2');
},
fun3:function(res){
console.log('我是fun3');
res.write('你好,我是fun3');
}
}
var http = require('http');
var ortherFun = require('./../otherjs/out.js');
http.createServer(function (request,response){
response.writeHead(200, {'Contet-Type':'text/html;charset=utf-8'});
if(request.url!=='/favicon.ico'){
// funl(response);
// ortherFun(response);
ortherFun.fun2(response);
ortherFun.fun3(response);
response.end('');
}
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');
function funl(res){
console.log('fun1');
res.write('hello ,我是fun1');
}
用字符串調(diào)用對應(yīng)的函數(shù)
var http = require('http');
var ortherFun = require('./../otherjs/out.js');
http.createServer(function (request,response){
response.writeHead(200, {'Contet-Type':'text/html;charset=utf-8'});
if(request.url!=='/favicon.ico'){
// funl(response);
// ortherFun(response);
//ortherFun.fun2(response);
//ortherFun.fun3(response);
// 用字符串調(diào)用對應(yīng)的函數(shù)
//ortherFun['fun2'](response);
//ortherFun['fun3'](response);
// 還可以寫成下面這樣
funname = 'fun2';
ortherFun[funname](response);
response.end('');
}
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');
function funl(res){
console.log('fun1');
res.write('hello ,我是fun1');
}


總結(jié)
以上所述是小編給大家介紹的nodejs中函數(shù)的調(diào)用實例詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
nodejs發(fā)送http請求時遇到404長時間未響應(yīng)的解決方法
這篇文章主要為大家詳細介紹了nodejs發(fā)送http請求時遇到404長時間未響應(yīng)的解決方法2017-12-12
在Node.js下運用MQTT協(xié)議實現(xiàn)即時通訊及離線推送的方法
這篇文章主要介紹了在Node.js下運用MQTT協(xié)議實現(xiàn)即時通訊及離線推送的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01
詳解NodeJS Https HSM雙向認證實現(xiàn)
這篇文章主要介紹了詳解NodeJS Https HSM雙向認證實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-03-03

