Skip to content

Quick start

You can install Mapl from npm.

Terminal window
npm i @mapl/web

Create a new file and add the following code:

server.ts
import { compile, router } from '@mapl/web';
// Create a router
const app = router()
// Add a route that matches GET requests to '/'
.get('/', () => 'Hi');
// Export the function to be executed
export default { fetch: compile(app) };

Then run the file with Bun or Deno.

Terminal window
# Bun
bun server.ts
# Deno
deno serve server.ts --port 3000

And done! We just build a simple web server running on 127.0.0.1:3000.

Explanation

  1. First we create a router instance by calling router().

    const app = router();
  2. 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');
  3. Then we use the compiler to compile to a request handler.

    const fetch = compile(app);
  4. Finally we export the function correctly for the runtime to execute.