- Introduced `ServerNode` and `BunServer` classes to handle server logic for Node and Bun respectively. - Updated `App` class to initialize the appropriate server based on the runtime environment. - Enhanced `parseBody` function to handle request body parsing for both environments. - Modified WebSocket handling to support Bun's WebSocket upgrade mechanism. - Improved error handling and response structure across the server implementation. - Added support for custom middleware in the server's request handling. - Refactored server base functionality into `ServerBase` for better code organization. - Updated type definitions to reflect changes in server options and listener handling. - Added a new demo for testing the server functionality with various request types.
38 lines
843 B
TypeScript
38 lines
843 B
TypeScript
import { Route, App } from '@kevisual/router/src/app.ts';
|
|
|
|
const app = new App({ serverOptions: { 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`);
|
|
|
|
app.server.on({
|
|
id: 'abc',
|
|
path: '/ws',
|
|
io: true,
|
|
fun: async ({ data }, { end }) => {
|
|
console.log('Custom middleware for /ws');
|
|
console.log('Data received:', data);
|
|
end({ message: 'Hello from /ws middleware' });
|
|
}
|
|
}) |