Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F7541603
server.js
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
2 KB
Referenced Files
None
Subscribers
None
server.js
View Options
import
{
html
}
from
'./html'
;
import
meow
from
'./meow.txt'
;
import
{
Radix3
}
from
'./radix3'
;
const
router
=
new
Radix3
();
router
.
insert
(
'/'
,
c
=>
{
return
c
.
res
.
body
(
`Welcome to Ant HTTP Server with Radix3 Router!
Available routes:
GET /
GET /meow
GET /hello
GET /status
GET /users/:id
GET /users/:id/posts
GET /files/*path
GET /api/v1/users
GET /api/v2/demo`
);
});
router
.
insert
(
'/meow'
,
async
c
=>
{
return
c
.
res
.
body
(
meow
);
});
router
.
insert
(
'/hello'
,
async
c
=>
{
return
c
.
res
.
body
(
'Hello, World!'
);
});
router
.
insert
(
'/status'
,
async
c
=>
{
return
c
.
res
.
body
(
'Server is running with Radix3 router!'
);
});
router
.
insert
(
'/users/:id'
,
async
c
=>
{
return
c
.
res
.
body
(
`User ID:
${
c
.
params
.
id
}
`
);
});
router
.
insert
(
'/users/:id/posts'
,
async
c
=>
{
return
c
.
res
.
body
(
`Posts for user:
${
c
.
params
.
id
}
`
);
});
router
.
insert
(
'/api/v1/users'
,
async
c
=>
{
return
c
.
res
.
json
({
users
:
[]
});
});
router
.
insert
(
'/api/v2/demo'
,
async
c
=>
{
return
c
.
res
.
json
({
slideshow
:
{
author
:
'Yours Truly'
,
date
:
'date of publication'
,
slides
:
[
{
title
:
'Wake up to WonderWidgets!'
,
type
:
'all'
},
{
items
:
[
'Why <em>WonderWidgets</em> are great'
,
'Who <em>buys</em> WonderWidgets'
],
title
:
'Overview'
,
type
:
'all'
}
],
metadata
:
{
title
:
'Sample Slide Show'
,
isFavorite
:
true
,
viewCount
:
105407
,
createdAt
:
'2024-12-01T22:51:49.000Z'
}
}
});
});
router
.
insert
(
'/files/*path'
,
async
c
=>
{
return
c
.
res
.
html
(
html
`<div>
${
c
.
params
.
path
}
</div>`
);
});
router
.
printTree
();
console
.
log
(
''
);
async
function
handleRequest
(
req
,
res
)
{
console
.
log
(
'request:'
,
req
.
method
,
req
.
uri
);
const
result
=
router
.
lookup
(
req
.
uri
);
if
(
result
?
.
handler
)
{
const
ctx
=
{
req
,
res
,
params
:
result
.
params
};
return
void
result
.
handler
(
ctx
);
}
res
.
body
(
'not found: '
+
req
.
uri
,
404
);
}
console
.
log
(
'started on http://localhost:8000'
);
Ant
.
serve
(
8000
,
handleRequest
);
File Metadata
Details
Attached
Mime Type
application/javascript
Expires
Wed, Jun 17, 2:30 PM (16 h, 47 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
573568
Default Alt Text
server.js (2 KB)
Attached To
Mode
rANT Ant
Attached
Detach File
Event Timeline
Log In to Comment