Quick start
You can install Mapl from npm.
npm i @mapl/app
Create a new file and add the following code:
import { jitc, router } from '@mapl/app';
// Create a routerconst app = router() // Add a route that matches GET requests to '/' .get('/', () => 'Hi');
// Build the appconst result = await jitc(app);
// Export the function to be executedexport default { fetch: result.fetch };
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 create an object that contains a request handler.
const result = await jitc(app);result.fetch; // This is the request handler -
Finally we export the function correctly for the runtime to execute.