Docs
CLI
Migrations
Compare
Benchmarks
Examples

© 2024 MoroJs

Getting Started

Quick Start

One command scaffolds a typed, validated MoroJS API. You'll have a running server in under a minute.

What You'll Learn

  • Scaffold a MoroJS project with the CLI
  • Run the dev server and hit your first endpoints
  • Add a typed, validated route

Scaffold Your Project

Run the CLI. You'll be prompted for runtime, validation library, and optional features — press enter to accept defaults for a typed REST API.

Scaffold

bash

1npx @morojs/cli init my-api

What this does: Creates my-api/ with TypeScript, ESM, validation, and a working src/index.ts wired up. The CLI auto-detects your package manager and installs dependencies.

Run It

Start the dev server with hot reload:

Start dev server

bash

1cd my-api
2npm run dev

You'll see the server come up on port 3000. Hit the welcome and health endpoints already scaffolded for you:

  • GET http://localhost:3000/ — welcome endpoint
  • GET http://localhost:3000/health — health check

Add a Typed, Validated Route

Open src/index.ts and add a parameterized GET and a validated POST. Save — the dev server reloads automatically.

src/index.ts

typescript

1import { z } from 'zod';
2
3// Parameterized route — req.params.id is typed as string
4app.get('/users/:id').handler((req) => {
5  return {
6    userId: req.params.id,
7    message: `User ${req.params.id} retrieved`,
8  };
9});
10
11// POST route with body validation (chainable pattern)
12const CreateUserSchema = z.object({
13  name: z.string().min(1),
14  email: z.string().email(),
15  age: z.number().min(18).max(120),
16});
17
18app.post('/users')
19  .validate({ body: CreateUserSchema })
20  .handler((req) => {
21    // req.body is fully typed and validated
22    return {
23      message: 'User created',
24      user: {
25        id: crypto.randomUUID(),
26        ...req.body,
27        createdAt: new Date().toISOString(),
28      },
29    };
30  });

Result: TypeScript knows the shape of req.body from the Zod schema. Invalid requests are rejected automatically before your handler runs.

Prefer to set up MoroJS by hand instead of using the CLI?

Manual installation guide

Next Steps