init router
This commit is contained in:
41
demo/simple/src/app-02.ts
Normal file
41
demo/simple/src/app-02.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { App } from '@abearxiong/router';
|
||||
|
||||
const app = new App();
|
||||
|
||||
app.listen(4002, () => {
|
||||
console.log('Server is running at http://localhost:4002');
|
||||
});
|
||||
const callback = (req, res) => {
|
||||
if (req.url.startsWith('/api/v')) {
|
||||
// 在这里处理 /api/v 的请求
|
||||
// res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||
setTimeout(() => {
|
||||
res.end('Intercepted /api/v request');
|
||||
}, 2000);
|
||||
}
|
||||
};
|
||||
|
||||
app.server.on(callback);
|
||||
|
||||
new app.Route('demo', '01')
|
||||
.define(async (ctx) => {
|
||||
ctx.body = '01';
|
||||
return ctx;
|
||||
})
|
||||
.addTo(app);
|
||||
|
||||
app
|
||||
.route('demo')
|
||||
.define(async (ctx) => {
|
||||
ctx.body = '02';
|
||||
return ctx;
|
||||
})
|
||||
.addTo(app);
|
||||
|
||||
app
|
||||
.route('demo', '03')
|
||||
.define(async (ctx) => {
|
||||
ctx.body = '03';
|
||||
return ctx;
|
||||
})
|
||||
.addTo(app);
|
||||
27
demo/simple/src/app-ws.ts
Normal file
27
demo/simple/src/app-ws.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { Route, App } from '@abearxiong/router';
|
||||
|
||||
const app = new App({ io: true });
|
||||
app.listen(4002);
|
||||
const route01 = new Route('demo', '01');
|
||||
route01.run = async (ctx) => {
|
||||
ctx.body = '01';
|
||||
return ctx;
|
||||
};
|
||||
app.use(
|
||||
'demo',
|
||||
async (ctx) => {
|
||||
ctx.body = '01';
|
||||
return ctx;
|
||||
},
|
||||
{ key: '01' },
|
||||
);
|
||||
|
||||
const route02 = new Route('demo', '02');
|
||||
route02.run = async (ctx) => {
|
||||
ctx.body = '02';
|
||||
return ctx;
|
||||
};
|
||||
app.addRoute(route02);
|
||||
|
||||
console.log(`http://localhost:4002/api/router?path=demo&key=02`);
|
||||
console.log(`http://localhost:4002/api/router?path=demo&key=01`);
|
||||
27
demo/simple/src/app.ts
Normal file
27
demo/simple/src/app.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { Route, App } from '@abearxiong/router';
|
||||
|
||||
const app = new App();
|
||||
app.listen(4003);
|
||||
const route01 = new Route('demo', '01');
|
||||
route01.run = async (ctx) => {
|
||||
ctx.body = '01';
|
||||
return ctx;
|
||||
};
|
||||
app.use(
|
||||
'demo',
|
||||
async (ctx) => {
|
||||
ctx.body = '01';
|
||||
return ctx;
|
||||
},
|
||||
{ key: '01' },
|
||||
);
|
||||
|
||||
const route02 = new Route('demo', '02');
|
||||
route02.run = async (ctx) => {
|
||||
ctx.body = '02';
|
||||
return ctx;
|
||||
};
|
||||
app.addRoute(route02);
|
||||
|
||||
console.log(`http://localhost:4003/api/router?path=demo&key=02`);
|
||||
console.log(`http://localhost:4003/api/router?path=demo&key=01`);
|
||||
36
demo/simple/src/index.ts
Normal file
36
demo/simple/src/index.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { QueryRouter, Route, Server } from '@abearxiong/router';
|
||||
|
||||
const router = new QueryRouter();
|
||||
|
||||
const route01 = new Route('demo', '01');
|
||||
route01.run = async (ctx) => {
|
||||
ctx.body = '01';
|
||||
return ctx;
|
||||
};
|
||||
router.add(route01);
|
||||
|
||||
const server = new Server({
|
||||
handle: async (msg) => {
|
||||
const res = await router.parse(msg);
|
||||
const { code, body, message } = res;
|
||||
// console.log('response', res);
|
||||
return { code, data: body, message };
|
||||
}
|
||||
});
|
||||
|
||||
// server.setHandle(async (msg) => {
|
||||
// const res = await router.parse(msg);
|
||||
// const { code, body, message } = res;
|
||||
// // console.log('response', res);
|
||||
// return { code, data: body, message };
|
||||
// });
|
||||
|
||||
server.listen(3000);
|
||||
|
||||
const route02 = new Route('demo', '02');
|
||||
route02.run = async (ctx) => {
|
||||
ctx.body = '02';
|
||||
return ctx;
|
||||
};
|
||||
router.add(route02);
|
||||
|
||||
85
demo/simple/src/validator/app.ts
Normal file
85
demo/simple/src/validator/app.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
import { Route, QueryRouter, RouteContext } from '@abearxiong/router';
|
||||
const qr = new QueryRouter();
|
||||
|
||||
qr.add(
|
||||
new Route('project', 'getList', {
|
||||
description: 'get project list',
|
||||
run: async (ctx) => {
|
||||
ctx!.body = 'project list';
|
||||
return ctx;
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
qr.add(
|
||||
new Route('project', 'getDetail', {
|
||||
description: 'get project detail',
|
||||
run: async (ctx) => {
|
||||
ctx!.body = 'project detail';
|
||||
return ctx;
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
qr.add(
|
||||
new Route('project', 'getDetail2', {
|
||||
description: 'get project detail2',
|
||||
run: async (ctx: RouteContext) => {
|
||||
ctx!.body = 'project detail2';
|
||||
return ctx;
|
||||
},
|
||||
validator: {
|
||||
id: {
|
||||
type: 'number',
|
||||
required: true,
|
||||
message: 'id is required',
|
||||
},
|
||||
data: {
|
||||
// @ts-ignore
|
||||
type: 'object',
|
||||
message: 'data query is error',
|
||||
properties: {
|
||||
name: {
|
||||
type: 'string',
|
||||
required: true,
|
||||
message: 'name is required',
|
||||
},
|
||||
age: {
|
||||
type: 'number',
|
||||
required: true,
|
||||
message: 'age is error',
|
||||
},
|
||||
friends: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
hair: {
|
||||
type: 'string',
|
||||
required: true,
|
||||
message: 'hair is required',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
const main = async () => {
|
||||
// 调用要测试的函数
|
||||
const res = await qr.parse({
|
||||
path: 'project',
|
||||
key: 'getDetail2',
|
||||
id: 4,
|
||||
data: {
|
||||
name: 'john',
|
||||
age: 's'+13,
|
||||
friends: {
|
||||
hair: 'black',
|
||||
messages: 'hello',
|
||||
},
|
||||
},
|
||||
} as any);
|
||||
console.log('test===', res);
|
||||
};
|
||||
main();
|
||||
Reference in New Issue
Block a user