MIRROR: javascript for ๐Ÿœ's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

server.cjs finally works

+13 -10
+13 -10
tests/server/server.cjs
··· 2 2 const router = new Radix3(); 3 3 4 4 router.insert('/', c => { 5 - return c.body(`Welcome to Ant HTTP Server with Radix3 Router! 5 + return c.res.body(`Welcome to Ant HTTP Server with Radix3 Router! 6 6 7 7 Available routes: 8 8 GET / ··· 16 16 }); 17 17 18 18 router.insert('/hello', async c => { 19 - return c.body('Hello, World!'); 19 + return c.res.body('Hello, World!'); 20 20 }); 21 21 22 22 router.insert('/status', async c => { 23 - return c.body('Server is running with Radix3 router!'); 23 + return c.res.body('Server is running with Radix3 router!'); 24 24 }); 25 25 26 26 router.insert('/users/:id', async c => { 27 - return c.body(`User ID: ${c.params.id}`); 27 + return c.res.body(`User ID: ${c.params.id}`); 28 28 }); 29 29 30 30 router.insert('/users/:id/posts', async c => { 31 - return c.body(`Posts for user: ${c.params.id}`); 31 + return c.res.body(`Posts for user: ${c.params.id}`); 32 32 }); 33 33 34 34 router.insert('/api/v1/users', async c => { 35 - return c.json({ users: null }); 35 + return c.res.json({ users: null }); 36 36 }); 37 37 38 38 router.insert('/api/v2/users', async c => { 39 - return c.json({ users: [] }); 39 + return c.res.json({ users: [] }); 40 40 }); 41 41 42 42 router.insert('/files/*path', async c => { 43 - return c.html(p.params.path); 43 + return c.res.html(c.params.path); 44 44 }); 45 45 46 46 router.printTree(); ··· 48 48 49 49 async function handleRequest(req, res) { 50 50 Ant.println('request:', req.method, req.uri); 51 + const result = router.lookup(req.uri); 51 52 52 - const result = router.lookup(req.uri); 53 - if (result?.handler) return result.handler(result.params); 53 + if (result?.handler) { 54 + const ctx = { req, res, params: result.params }; 55 + return result.handler(ctx); 56 + } 54 57 55 58 return res.body('not found: ' + req.uri, 404); 56 59 }