nodejs中函數(shù)的調(diào)用實(shí)例詳解
一、調(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');
}
運(yùn)行結(jié)果:


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


function fun2(res){
console.log('我是,fun2');
res.write('你好我是fun2');
}
// 想把此js聲明為一個(gè)函數(shù),加下面代碼,只適用于文件中只有一個(gè)函數(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)有多個(gè)函數(shù)
// 支持多個(gè)函數(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)用對(duì)應(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)用對(duì)應(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)用實(shí)例詳解,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
nodejs發(fā)送http請(qǐng)求時(shí)遇到404長時(shí)間未響應(yīng)的解決方法
這篇文章主要為大家詳細(xì)介紹了nodejs發(fā)送http請(qǐng)求時(shí)遇到404長時(shí)間未響應(yīng)的解決方法2017-12-12
npm 更改默認(rèn)全局路徑以及國內(nèi)鏡像的方法
今天小編就為大家分享一篇npm 更改默認(rèn)全局路徑以及國內(nèi)鏡像的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-05-05
在Node.js下運(yùn)用MQTT協(xié)議實(shí)現(xiàn)即時(shí)通訊及離線推送的方法
這篇文章主要介紹了在Node.js下運(yùn)用MQTT協(xié)議實(shí)現(xiàn)即時(shí)通訊及離線推送的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01
詳解NodeJS Https HSM雙向認(rèn)證實(shí)現(xiàn)
這篇文章主要介紹了詳解NodeJS Https HSM雙向認(rèn)證實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-03-03
node.js對(duì)應(yīng)npm安裝和使用方法教程
這篇文章主要給大家介紹了關(guān)于node.js對(duì)應(yīng)npm安裝和使用方法的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用node.js具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-01-01

