路由模块
#
1.编写路由import { Response, Request, Module } from '@markstien/fly';import { mainHandler } from "./main.handler";
export const mainRouting: Module = { getHello: { path: '/', method: 'GET', handler: function (request: Request,response: Response) { response.sendText('Hello,world!'); } }, getUser: { path: '/user', method: 'GET', handler: function (request: Request,response: Response) { // @ts-ignore const user = mainHandler.getUser(request.params['id']); response.sendText(JSON.stringify(user)); } }}
#
2.实现处理函数interface User { id: string; name: string; age: number;}
export const mainHandler = { getUser(id: string): User { return { id:id, age:20, name: `user-${id}`}; }}
#
3. 启动服务器import { createFly } from '@markstien/fly';import { mainRouting } from "./src/routings/main/main.routing";
async function fly() { const app = createFly([ mainRouting ]); app.enableCROS();
await app.start(9000); console.log('http://localhost:9000');}
fly();
文件结构如下: