node(一) hello,world

1_hello.js代码:

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此访问  
        console.log('访问');  
        response.write('hello,world');  
        response.end();//不写则没有http协议尾,但写了会产生两次访问  
    }  
}).listen(8000);  
console.log('Server  running  at  http://localhost:8000/');  

/*  
启动服务  
cmd下执行:  
node  1_hello.js  
浏览器访问:http://localhost:8000  
*/


 

内部函数调用

2_fun.js代码:

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:8000/');    
function  fun1(res){      
    res.write("你好,我是fun1");      
}

导入外部js(一个函数)

3_fun.js代码:

var http = require('http');  
var otherfun = require('./3_models.js');//导入外部js
http.createServer(function(request,response){      
    response.writeHead(200,{'Content-Type':'text/html;charset=utf-8'});      
    if(request.url!=="/favicon.ico"){    //清除第2此访问  
        otherfun(request,response);
    }  
}).listen(8000);      
console.log('Server running at http://localhost:8000/');

同级目录3_models.js代码:

function controller(req,res){      
    res.write("调用函数写出...");      
    call('hello',req,res);      
    res.end();      
}      
function call(res){      
    console.log('调用call函数');      
}      
module.exports = controller; //只支持一个函数

导入外部js(多个函数)

4_fun.js代码:

var http = require('http');  
var otherfun = require('./4_models.js');//导入外部js
http.createServer(function(request,response){      
    response.writeHead(200,{'Content-Type':'text/html;charset=utf-8'});      
    if(request.url!=="/favicon.ico"){    //清除第2此访问  
        otherfun.fun1(request,response);
        response.write(otherfun.fun2(1,2)+"");//必须是字符串
        response.write("</br>");
        otherfun['fun1'](request,response);
        response.write(otherfun['fun2'](1,2)+"");//必须是字符串
        response.write("</br>");
        var name = 'fun1';
        otherfun[name](request,response);
        response.end("完毕!");
    }  
}).listen(8000);      
console.log('Server running at http://localhost:8000/');

同级目录4_models.js代码:

//支持多个函数      
module.exports={      
    fun1:function(req,res){      
        res.write("调用函数fun1...");
    },      
    fun2:function(a,b){      
        return  a+b;  
    }      
}

模块调用

5_main.js代码:

var http = require('http');
var Student = require('./5_student.js');
var Teacher = require('./5_teacher.js');
http.createServer(function(request,response){      
    response.writeHead(200,{'Content-Type':'text/html;charset=utf-8'});      
    if(request.url!=="/favicon.ico"){    //清除第2此访问  
        var student = new Student(1,'张三',18);
        student.Stud(response);
        student.enter(response);
        response.write("</br>");
        var teacher = new Teacher(1,'李四',38);
        teacher.teach(response);
        teacher.enter(response);
        response.write("</br>");
        response.end('完毕!');       
    }  
}).listen(8000);      
console.log('Server running at http://localhost:8000/');

同级目录5_user.js代码:

function User(id,name,age){
    this.id=id;
    this.name=name;
    this.age=age;
    this.enter=function(res){
        res.write(this.name+"进入图书馆");//公共的方法
    }
}
module.exports = User;

同级目录5_student.js代码:

var User = require('./5_user');
function Student(id,name,age){
    User.apply(this,[id,name,age]);//继承5_user.js
    this.Stud=function(res){
        res.write(this.name+"学生在听课...");
    }
}
module.exports = Student;

同级目录5_teacher.js代码:

var User = require('./5_user');
function  Teacher(id,name,age){
    User.apply(this,[id,name,age]);//继承5_user.js
    this.teach=function(res){
        res.write(this.name+"老师讲课...");
    }
}
module.exports = Teacher;

 

posted @ 2017-12-12 14:34  爱做梦的奋斗青年  阅读(147)  评论(0编辑  收藏  举报