Commit d3b22d45 authored by 尚斌杰's avatar 尚斌杰

增加中间件屏蔽Ip的练习

parent 9462befa
'use strict';
module.exports = (option, app) => {
return async function forbidIp(ctx, next) {
const forbidip = '192.168.146.169';
console.log(ctx.request.ip);
if (ctx.request.ip === forbidip) {
ctx.status = 403;
ctx.body = '您的Ip已经被屏蔽';
} else {
await next();
}
};
};
'use strict';
/**
* @param {*} options 入参
* @param {*} app application实例
*/
module.exports = options => {
// 返回一个异步的方法
return async function priontDate(ctx, next) {
console.log(new Date(), options.name);
await next();
};
};
...@@ -7,8 +7,10 @@ module.exports = appInfo => { ...@@ -7,8 +7,10 @@ module.exports = appInfo => {
config.keys = appInfo.name + '_1537794513145_4789'; config.keys = appInfo.name + '_1537794513145_4789';
// add your config here // add your config here
config.middleware = []; config.middleware = [ 'printdate', 'forbidip' ];
config.printdate = {
name: '中间件',
};
// 模板引擎 // 模板引擎
config.view = { config.view = {
mapping: { mapping: {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment