Quick start
You can install Mapl from npm.
npm i @mapl/web
Create a new file and add the following code:
import { compile, router } from '@mapl/web';
// Create a routerconst app = router() // Add a route that matches GET requests to '/' .get('/', () => 'Hi');
// Export the function to be executedexport default { fetch: compile(app) };
Then run the file with Bun or Deno.
# Bunbun server.ts
# Denodeno serve server.ts --port 3000
And done! We just build a simple web server running on 127.0.0.1:3000.
Explanation
-
First we create a router instance by calling
router()
.const app = router(); -
Then we add an endpoint that returns
Hi
as a response.const app = router().get('/', () => 'Hi');You can return anything that is supported by the Response constructor, which includes
string
,ArrayBuffer
,Blob
,ReadableStream
, …Chaining is also possible since every method returns the current app instance.
const app = router().get('/', () => 'Hi').get('/hello', () => 'Hello'); -
Then we use the compiler to compile to a request handler.
const fetch = compile(app); -
Finally we export the function correctly for the runtime to execute.