func1.js代码:
function fun1(res){
res.write("fun1 is run<br>");
}
module.exports=fun1; //对外暴露才能被调用
func2.js代码:
module.exports={
fun2:function(res){
res.write("fun2 if run<br>");
},
fun3:function(res){
res.write("fun3 if run<br>");
}
}
ceshi.js代码:
var http = require('http');
var impfunc=require('./func1.js');//调用只有一个函数的js文件
var impfunc2=require('./func2.js');//调用多个函数的js文件
http.createServer(function (request,response){
response.writeHead(200,{'Content-Type':'text/html;charset=utf-8'});
//console.log(request.url);
if(request.url!="/favicon.ico"){ //清楚第二次访问
console.log('access log');
response.write('hello!!!<br>');
fun(response);
impfunc(response);
impfunc2.fun2(response);
impfunc2['fun3'](response);
//response.end('');//如果不加这一行,浏览器一致在转。代表本次访问结束。
response.end('结束');//如果不加这一行,浏览器一致在转。代表本次访问结束。
}
}).listen(8001);
console.log(' node server is run');
function fun(res){
res.write("fun is run<br>");
}