koa是Express的下一代基于node的web框架,express对node的http进行了封装:
var
express =
require(
'express');
var
app =
express();
app.
get(
'/',
function (
req,
res) {
res.
send(
'hello word!')
});
app.
listen(
3000,
function () {
onsole.
log(
'express listen at 3000')
})
app.
get(
'/test',
function (
req,
res) {
fs.
readFile(
'/file1',
function(
err,
data) {
if (
err) {
res.
status(
500).
send(
'read file1 error')
}
fs.
readFile(
'/file2',
function (
err,
data) {
if (
err) {
res.
status(
500).
send(
'read file2 error')
}
res.
type(
'text/plain')
res.
send(
data);
})
})
})
随着新版node开始支持ES6,Express的团队又基于ES6的generator重新编写了下一代web框架koa。和express相比,koa1.0使用generator实现异步,代码看起来像同步的: