node基础03:使用函数

1.使用函数

//server.js

var http = require("http");
var output = require("./output");

http.createServer(function(request, response){
    response.writeHead(200, {"Content-Type":"text/html; charset=uf-8"});
    if (request.url!=="/favicon.ico") {
        console.log(output);
        output(request, response);
    }
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');
//output.js
function output(request, response){
    response.write('hello world');
    console.log('hello');
    response.end();
}

module.exports = output//只支持一个函数,通常情况下导出的为一个对象

/**
 module.exports = {
    fn1: fn1,
    fn2: fn2
 }

 在其他模块调用时,使用 别名.fnName()调用这里的fn1
 **/
posted on 2017-01-03 08:54  码先生  阅读(195)  评论(0编辑  收藏  举报