Open monkeydone opened 6 years ago
var events = require("events") eventEmitter = new events.EventEmitter();
eventEmitter.on('test',function(){}); eventEmitter.emit("test"); eventEmitter.emit('error');
var b = new Buffer(10); b.write("this is a test"); b.toJSON()
var fs = require('fs') var reader = fs.createReadStream('input.txt') var write = fs.createWriteStream('output.txt') reader.pipe(write)
fs.createReadStream('input.txt') .pipe(zlib.createGzip()) .pipe(fs.createWriteStream('input.txt.gz'));
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);
function route(pathname) {
console.log("About to route a request for " + pathname);
}
exports.route = route;
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;
util.inherits(constructor, superConstructor)是一个实现对象间原型继承 的函数。
var util = require('util');
function Base() {
this.name = 'base';
this.base = 1991;
this.sayHello = function() {
console.log('Hello ' + this.name);
};
}
Base.prototype.showName = function() {
console.log(this.name);
};
function Sub() {
this.name = 'sub';
}
util.inherits(Sub, Base);
var objBase = new Base();
objBase.showName();
objBase.sayHello();
console.log(objBase);
var objSub = new Sub();
objSub.showName();
//objSub.sayHello();
console.log(objSub);
util.inspect(object,[showHidden],[depth],[colors])是一个将任意对象转换 为字符串的方法,通常用于调试和错误输出。它至少接受一个参数 object,即要转换的对象。
var fs=require("fs")
hello world