Last active
August 29, 2015 14:10
-
-
Save dead-horse/e58dc33d026739a718d4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// error in next tick | |
try { | |
setImmediate(function () { | |
throw new Error('error happened'); | |
}); | |
} catch (err) { | |
// 捕获不到异常 | |
} | |
// get json by callback | |
function getJSON(file, callback) { | |
fs.readFile(file, function (err, content) { | |
if (err) return callback(err); | |
try { | |
content = JSON.parse(content); | |
} catch (err) { | |
return callback(err); | |
} | |
return callback(null, content); | |
}); | |
} | |
// callback hell | |
getUser(function (err, user) { | |
if (err) return callback(err); | |
getArticle(user, function (err, article) { | |
if (err) return callback(err); | |
getComment(article, function (err, comment)) { | |
if (err) return comment; | |
// res = ... | |
callback(null, res); | |
}); | |
}); | |
}); | |
// get json by co | |
co(function* getJSON(file) { | |
var content = yield fs.readFile(file); | |
content = JSON.parse(content); | |
}).catch(error); | |
// sync style in co | |
co(function* () { | |
var user = yield getUser(); | |
var article = yield getArticle(user); | |
var comment = yield getComment(article); | |
// res = ... | |
}).catch(error); | |
//hello koa | |
var koa = require('koa'); | |
var app = koa(); | |
app.use(function* () { | |
this.body = 'hello koa'; | |
}); | |
app.listen(3000); | |
// error handler in express | |
app.use(function (req, res, next) { | |
fs.readFile('package.json', function (err, content) { | |
if (err) return next(err); | |
try { | |
content = JSON.parse(content); | |
} catch (err) { | |
return next(err); | |
} | |
content.user = 'dead_horse'; | |
res.json(content); | |
}); | |
}); | |
app.use(function errorHandler (err, req, res, next) { | |
res.status(500).send(err.message); | |
}); | |
// error handle in koa | |
app.use(function* (next) { | |
try { | |
yield next; | |
} catch (err) { | |
this.body = err.message; | |
this.status = 500; | |
} | |
}); | |
app.use(function* () { | |
var content = yield fs.readFile('package.json'); | |
var content = JSON.parse(content); | |
content.user = 'dead_horse'; | |
this.body = content; | |
}); | |
// stream in express | |
app.use(function (req, res) { | |
var stream = fs.createReadStream('filename.txt'); | |
stream | |
.on('error', onerror) | |
.pipe(zlib.createGzip()) | |
.on('error', onerror) | |
.pipe(res); | |
res.once('close', function () { | |
// 如果客户端终止了这个请求,可能导致 `fd` 泄漏 | |
// 需要 `unpipe` 来让上游关闭这个 `fd` | |
stream.unpipe(); | |
}); | |
function onerror(err) { | |
res.status(500).send(err.message); | |
} | |
}); | |
// stream in koa | |
app.use(function* () { | |
this.body = fs.createReadStream('filename.txt'); | |
this.body = this.body.pipe(zlib.createGzip()); | |
}); | |
// middleware | |
app.use(function* responseTime(next) { | |
var start = Date.now(); | |
// 在进入下一个中间件前执行 | |
yield next; | |
// 在执行完后面的所有中间件之后执行 | |
var used = Date.now() - start; | |
this.set('X-Response-Time', used); | |
}); | |
app.use(function* respond() { | |
this.body = 'hello koa'; | |
this.status = 200; | |
}); | |
// middleware | |
app.use(function* () { | |
var start = Date.now(); | |
// yield next() => yield respond(); | |
this.body = 'hello koa'; | |
this.status = 200; | |
var used = Date.now() - start; | |
this.set('X-Response-Time', used); | |
}); | |
// delay | |
// 设置响应的 body | |
app.use(function* respond(next) { | |
this.body = fs.createReadStream(this.path); | |
yield next; | |
}); | |
// 设置 etag | |
app.use(function* etag(next) { | |
var stat = yield fs.stat(this.body.path); | |
this.response.etag = require('etag')(stat); | |
yield next; | |
}); | |
// 将 body gzip | |
app.use(function* gzip(next) { | |
this.body = this.body.pipe(zlib.createGzip()); | |
}); | |
app.use(function* () { | |
// 设置响应 body | |
this.body = fs.createReadStream(this.path); | |
// 设置 etag | |
var stat = yield fs.stat(this.body.path); | |
this.response.etag = require('etag')(stat); | |
// 修改响应 body (gzip 压缩) | |
this.body = this.body.pipe(zlib.createGzip()); | |
// koa 自带的最外层的中间件,向外写响应 | |
this.body.pipe(this.res); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment