學習Node.js-1


/*
var http = require("http");
http.createServer(function(request, response){
   //發送HTTP頭部
    //HTTP 狀態值:200 : ok
    //內容類型:text/plain
    response.writeHead(200, {"Content-Type":"text/plain"});

    //發送響應數據 「Hello World"
    response.end("Hello World\n");
}).listen(8888);

console.log("Server runing at http://127.0.0.1:88888/");*/

/*var fs = require("fs");

var data = fs.readFileSync("D:\\WebstormProjects\\node1\\input.txt");

console.log(data.toString());
console.log("程序執行結束!");*/

/*
var fs = require("fs");

fs.readFile("D:\\WebstormProjects\\node1\\input.txt", function(err, data){
   if(err){
       return console.error(err);
   } else{
       console.log(data.toString());
   }
});

console.log("程序執行結束");
*/
/*

//引入 events 模塊
var events = require("events");
//建立 eventEmitter 對象
var eventEmitter = new events.EventEmitter();
//建立事件處理程序
var connectHandler = function connected(){
    console.log("鏈接成功");
    // 觸發 data_received事件
    eventEmitter.emit("data_received");
};
// 綁定 connection 事件處理程序
eventEmitter.on("connection", connectHandler);
//使用匿名函數綁定 data_received 事件
eventEmitter.on("data_received", function(){
   console.log("數據接收成功");
});
// 觸發 connection 事件
eventEmitter.emit("connection");

console.log("程序執行完畢。");*/


/*
var EventEmitter = require("events").EventEmitter;

var event = new EventEmitter();
event.on("some_event", function(){
   console.log("some_event 事件觸發");
});
setTimeout(function(){
    event.emit("some_event");
}, 1000);*/

/*
var events = require("events");
var emitter = new events.EventEmitter();
emitter.on("someEvent", function(arg1, arg2){
    console.log("listener1", arg1, arg2);
});
emitter.on("someEvent", function(arg1, arg2){
    console.log("listener2", arg1, arg2);
});
emitter.emit("someEvent", "arg1 參數", "arg2 參數");
*/

/*
var events = require("events");
var eventEmitter = new events.EventEmitter();
//監聽器 #1
var listener1 = function listener1(){
    console.log("監聽器 listenter1 執行。");
}

//監聽器 #2
var listener2 = function listener2(){
    console.log("監聽器 listener2 執行。");
}

//綁定 connection 事件,處理函數爲 listener1
eventEmitter.addListener("connection", listener1);

//綁定 connection 事件,處理函數爲listener2
eventEmitter.on("connection", listener2);

var eventListeners = require("events").EventEmitter.listenerCount(eventEmitter, "connection");
console.log(eventListeners + " 個監聽器監聽鏈接事件");

//處理 connection 事件
eventEmitter.emit("connection");
//移除綁定的 listener1 函數
eventEmitter.removeListener("connection", listener1);
console.log("listener1 再也不受監聽。");

//觸發鏈接事件
eventEmitter.emit("connection");
eventListeners = require("events").EventEmitter.listenerCount(eventEmitter, "connection");
console.log(eventListeners + " 個監聽器監聽鏈接事件。");
console.log("程序執行完畢。");*/

/*
var events = require("events");
var emitter = new events.EventEmitter();
emitter.emit("error");*/
/*

var buf = new Buffer(256);
len = buf.write("www.runoob.com");
console.log("寫入字節數:" + len);

buf = new Buffer(26);
for(var i = 0; i < 26; i++){
    buf[i] = i + 97;
}
console.log(buf.toString("ascii"));//輸出:abcdefghijklmnopqrstuvwxyz
console.log(buf.toString("ascii", 0, 5)); // 輸出:abcde
console.log(buf.toString("utf8", 0, 5)); //輸出:abcde
console.log(buf.toString(undefined, 0, 5));//使用「utf8」編碼,並輸出abcde

var buf = new Buffer("www.runoob.com");
var json = buf.toJSON(buf);
console.log(json);

var buffer1 = new Buffer("菜鳥教程");
var buffer2 = new Buffer("www.runoob.com");
var buffer3 = Buffer.concat([buffer1, buffer2]);
console.log("buffer3 內容:" + buffer3.toString());

var buffer1 = new Buffer("ABC");
var buffer2 = new Buffer("ABCD");
var result = buffer1.compare(buffer2);
if(result < 0){
    console.log(buffer1 + " 在 " + buffer2  + "以前");
}else if(result == 0){
    console.log(buffer1 + " 在 " + buffer2  + "相同");
}else{
    console.log(buffer1 + " 在 " + buffer2  + "以後");
}

var buffer1 = new Buffer("ABC");
var buffer2 = new Buffer(3);
buffer1.copy(buffer2);
console.log("buffer2 content: " + buffer2.toString());

var buffer1 = new Buffer("runoob");
//剪切緩衝區
var buffer2 = buffer1.slice(0, 2);
console.log("buffer2 content: " + buffer2.toString());

var buffer = new Buffer("www.runoob.com");
console.log("buffer length: " + buffer.length);


*/
/*

var fs = require("fs");
var data = "";
//建立可讀流
var readerStream = fs.createReadStream("D:\\WebstormProjects\\node1\\input.txt");
//設置編碼爲 utf8
readerStream.setEncoding("UTF8");
//處理流事件 --> data, end, and error
readerStream.on("data", function(chunk){
   data += chunk;
});

readerStream.on("end", function(){
   console.log(data);
});

readerStream.on("error", function(err){
   console.log(err.stack);
});
console.log("程序執行完畢");

*/
/*

var fs = require("fs");
var data = "菜鳥教程官網地址:www.runoob.com";
//建立一個能夠寫入的流,寫入到文件 output.txt 中
var writerStream = fs.createWriteStream("output.txt");
//使用 utf8 編碼寫入數據
writerStream.write(data, "UTF8");
//標記文件末尾
writerStream.end();
//處理流事件 --> data, end, and error
writerStream.on("finish", function(){
   console.log("寫入完成。");
});
writerStream.on("error", function(err){
   console.log(err.statck);
});

console.log("程序執行完畢");
*/

/*
var fs = require("fs");
//建立一個可讀流
var readerStream = fs.creteReadStream("input.txt");
//建立一個可寫流
var writerStream = fs.createWriteStream("output.txt");
//管道讀寫操做
//讀取input.txt文件內容,並將內容寫入output.txt文件中
readerStream.pipe(writerStream);
console.log("程序執行完畢");*/

/*
var fs = require("fs");
var zlib = require("zlib");
//壓縮 input.txt 文件爲 input.txt.gz
fs.createReadStream("input.txt")
.pipe(zlib.createGzip())
.pipe(fs.createWriteStream("input.txt.gz"));
console.log("文件壓縮完成。");*/

/*
var fs = require("fs");
var zlib = require("zlib");
//解壓 input.txt.gz 文件爲 input.txt
fs.createReadStream("input.txt.gz")
    .pipe(zlib.createGunzip())
    .pipe(fs.createWriterStream("input.txt"));

console.log("文件解壓完成。");
*/

/*
var Hello= require("./hello");
hello = new Hello();
hello.setName("BYVoid");
hello.sayHello();
*/
/*

var http = require("http");
function onRequest(request, response){
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.write("Hello World");
    response.end();
}
http.createServer(onRequest).listen(8888);
*/
/*

var http = require("http");
var url = require("url");

function start(route){
    function onRequest(request, response){
        var pathname = url.parse(request.url).pathname;
        console.log("Request for " + pathname + " received.");
        route(pathname);
        response.writeHead(200, {"Content-Type": "text/plain"});
        response.write("Hello World");
        response.end();
    }

    http.createServer(onRequest).listen(8888);
    console.log("Server has started.");
}

exports.start = start;*/
/*

console.log(__filename);
console.log(__dirname);

function printHello(){
    console.log("Hello, World!");
}
//兩秒後執行以上函數
var t = setTimeout(printHello, 2000);
//清除定時器
clearTimeout(t);

function printHello(){
    console.log("Hello, World!");
}
//兩秒後執行以上函數
setInterval(printHello, 2000);*/

/*

console.info("程序開始執行:");
var counter = 10;
console.log("計數:%d", counter);
console.time("獲取數據");
console.timeEnd("獲取數據");
console.info("程序執行完畢。");*/

/*

process.on("exit", function(code){
    //如下代碼永遠不會執行
    setTimeout(function () {
        console.log("該代碼不會執行");
    }, 0);
    console.log("退出碼爲:" + code);
});
console.log("程序執行結束");
*/

index.jsnode

var server = require("./server");
var router = require("./router");

server.start(router.route);

router.jsjson

function route(pathname){
    console.log("About to route a request for " + pathname);
}
exports.route = route;

hello.js函數

function Hello(){
    var name;
    this.setName = function(thyName){
        name = thyName;
    };
    this.sayHello = function(){
        console.log("Hello " + name);
    };
};
module.exports = Hello;
相關文章
相關標籤/搜索