Koa 框架
重要通知
。
基本概况
Koa是由Express背后的团队设计的新Web框架, 旨在成为一个更小、更具表现力、更强大的基础 适用于 Web 应用程序和 API。通过利用异步功能,Koa允许您 放弃回调并大大增加错误处理。Koa不捆绑任何 其核心中的中间件,它提供了一套优雅的方法,使 编写服务器快速而愉快。
代码示例
const Koa = require('koa');
const Router = require('koa-router');
const { koaBody } = require('koa-body');
const proxy = require('koa-proxy');
const cors = require('koa-cors');
global.koaApp = new Koa();
global.koaRouter = new Router();
//通信配置
global.koaBody = koaBody;
koaApp.use(koaBody());
// 反向代理
koaApp.use(proxy({
host: 'http://localhost:7061'
}));
//跨域配置
koaApp.use(cors());
//路由配置
koaApp.use(koaRouter.routes());
const port = 7062;
koaApp.listen(port);
console.log(`
··································${new Date()}··································
PORT: ${port} Server is Running...
http://localhost:${port}
server is starting ...
...
`);
/**
* Koa异常机制
*/
koaApp.on('error', err => {
//err.stack
console.log(`\n\n Koa异常错误机制 \n ${err} \n\n`);
});
koa-router
koaRouter.get("/", async (ctx, next) => {
const resDt = { status: 200, msg: "success", data: { token: Date.now() } };
const query = ctx.request.query;
const method = ctx.request.method;
const header = ctx.request.header;
const url = ctx.request.url;
resDt.data['extends'] = { method: method, url: url };
ctx.body = resDt;
process.stdout.write(`${JSON.stringify(resDt)}\n\n`);
});
koaRouter.post("/api/post", async (ctx, next) => {
const resDt = { status: 200, msg: "success", data: { token: Date.now() } };
const body = ctx.request.body;
const method = ctx.request.method;
const header = ctx.request.header;
const url = ctx.request.url;
resDt.data['extends'] = { method: method, url: url };
ctx.body = resDt;
process.stdout.write(`${JSON.stringify(resDt)}\n\n`);
});
生态系统
koa2-cors
代码示例
var Koa = require('koa');
var cors = require('koa2-cors');
var app = new Koa();
//跨域配置 注意:nginx配置Access-Control-Allow-Origin'后会导致:The 'Access-Control-Allow-Origin' header contains multiple values ' *',
const cors = require('koa2-cors');
app.use(cors());
app.use(cors({
origin: function(ctx) {
if (ctx.url === '/test') {
return false;
}
return '*';
},
exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'],
// Access-Control-Max-Age
maxAge: 5,
credentials: true,
// 默认 ['GET', 'PUT', 'POST', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS']
allowMethods: ['GET', 'POST', 'DELETE', 'PUT'],
allowHeaders: ['Content-Type', 'Authorization', 'Accept'],
}));
koa-body
multipart/form-data
application/x-www-form-urlencoded
application/json
application/json-patch+json
application/vnd.api+json
application/csp-report
text/xml
- 基本语法
koaBody({
patchNode: // {Boolean} Patch request body to Node's ctx.req, default false
patchKoa: {Boolean} Patch request body to Koa's ctx.request, default true
jsonLimit: {String|Integer} The byte (if integer) limit of the JSON body, default 1mb
formLimit: {String|Integer} The byte (if integer) limit of the form body, default 56kb
textLimit: {String|Integer} The byte (if integer) limit of the text body, default 56kb
encoding: {String} Sets encoding for incoming form fields, default utf-8
multipart: {Boolean} Parse multipart bodies, default false
urlencoded: {Boolean} Parse urlencoded bodies, default true
text: {Boolean} Parse text bodies, such as XML, default true
json: {Boolean} Parse JSON bodies, default true
jsonStrict: {Boolean} Toggles co-body strict mode; if set to true - only parses arrays or objects, default true
includeUnparsed: {Boolean} Toggles co-body returnRawBody option; if set to true, for form encodedand and JSON requests the raw, unparsed requesty body will be attached to ctx.request.body using a Symbol, default false
formidable: {Object} Options to pass to the formidable multipart parser
onError: {Function} Custom error handle, if throw an error, you can customize the response - onError(error, context), default will throw
strict: {Boolean} DEPRECATED If enabled, don't parse GET, HEAD, DELETE requests, default true
parsedMethods: {String[]} Declares the HTTP methods where bodies will be parsed, default ['POST', 'PUT', 'PATCH']. Replaces strict option.
});
formidable: {
maxFields: {Integer} Limits the number of fields that the querystring parser will decode, default 1000
maxFieldsSize: {Integer} Limits the amount of memory all fields together (except files) can allocate in bytes. If this value is exceeded, an 'error' event is emitted, default 2mb (2 * 1024 * 1024)
uploadDir: {String} Sets the directory for placing file uploads in, default os.tmpDir()
keepExtensions: {Boolean} Files written to uploadDir will include the extensions of the original files, default false
hash: {String} If you want checksums calculated for incoming files, set this to either 'sha1' or 'md5', default false
multiples: {Boolean} Multiple file uploads or no, default true
onFileBegin: {Function} Special callback on file begin. The function is executed directly by formidable. It can be used to rename files before saving them to disk.
}
koa-static
koa-proxy
npm install koa-proxy -S
const proxy = require('koa-proxy');
// 重定向到 http://alicdn.com
koaApp.use(proxy({
host: 'http://alicdn.com'
}));
// 访问index.js重定向到'http://alicdn.com/index.js
koaApp.get('index.js', proxy({
url: 'http://alicdn.com/index.js'
}));
// 指定一个键/值对象,该对象可以将请求的路径映射到另一个对象
koaApp.get('index.js', proxy({
host: 'http://alicdn.com',
map: {
'index.js': 'index-1.js'
}
}));
// 指定一个函数,该函数可以将请求的路径映射到所需的目标
koaApp.get('index.js', proxy({
host: 'http://alicdn.com',
map: function(path) { return 'public/' + path; }
}));
// 指定匹配标准以将代理调用限制为给定路径
app.use(proxy({
host: 'http://alicdn.com',
match: /^\/static\//
}));
// 使用 match 来排除特定路径
app.use(proxy({
host: 'http://alicdn.com',
match: /^(?!\/dontproxy\.html)/
}));
// 通过设置 来配置代理以记住 Cookie 以供将来使用 。这意味着服务器设置的 cookie 将被存储并在后续请求中重新发送。
app.use(proxy({
jar: true,
}));
// 代理不会向真实服务器发送 foo 和 bar' 标头,也不会从真实服务器接收 jar-jar。
app.use(proxy({
suppressRequestHeaders: ['foo','bar'], // case-insensitive
suppressResponseHeaders: ['jar-jar'] // case-insensitive
}));
// 向响应添加新标头或覆盖现有标头
app.use(proxy({
overrideResponseHeaders: {
"cow": "moo",
"duck": "quack"
},
}));