Features
Docs
CLI
Benchmarks
Examples

© 2024 MoroJs

Quick Start

Get up and running with MoroJS in minutes. This guide will walk you through creating your first high-performance API.

What You'll Learn

  • How to set up your first MoroJS project
  • Create your first API endpoint
  • Add validation and type safety
  • Run and test your API

Prerequisites

  • Node.js 18+ installed
  • Basic TypeScript knowledge
  • Package manager (npm, yarn, or pnpm)
1

Installation

ESM-Only Framework

MoroJS is built for modern JavaScript and requires ESM (ES Modules). Make sure your project uses "type": "module" in package.json.

Install MoroJS using your preferred package manager:

npm

bash

1npm install @morojs/moro

yarn

bash

1yarn add @morojs/moro

pnpm

bash

1pnpm add @morojs/moro

Create your project structure:

Create Your Project

bash

1# Create a new project
2mkdir my-moro-api && cd my-moro-api
3
4# Initialize package.json
5npm init -y && \
6npm pkg set type=module scripts.dev="tsx watch src/server.ts" scripts.build="tsc" scripts.start="node dist/server.js" && \
7npm install -D tsx typescript @types/node && \
8npm install @morojs/moro
9
10# Create tsconfig.json (ESM configuration)
11cat > tsconfig.json << 'EOF'
12{
13  "compilerOptions": {
14    "target": "ES2022",
15    "module": "NodeNext",
16    "moduleResolution": "NodeNext",
17    "esModuleInterop": true,
18    "allowSyntheticDefaultImports": true,
19    "strict": true,
20    "skipLibCheck": true,
21    "forceConsistentCasingInFileNames": true,
22    "outDir": "./dist",
23    "rootDir": "./src"
24  },
25  "include": ["src/**/*"],
26  "exclude": ["node_modules", "dist"]
27}
28EOF

✓ What this does: Sets up a new project with ESM support and TypeScript configuration.

2

Your First API

Create a new file src/server.ts and add your first route:

src/server.ts

typescript

1import { createApp } from '@morojs/moro';
2
3// Create a new MoroJS application
4const app = createApp({
5  server: {
6    port: 3000,
7    host: '0.0.0.0',
8  },
9});
10
11// Define a simple route
12app.get('/').handler(() => {
13  return { 
14    message: 'Hello from MoroJS!',
15    timestamp: new Date().toISOString(),
16    version: '1.0.0'
17  };
18});
19
20// Add a parameterized route
21app.get('/users/:id').handler(({ params }) => {
22  return {
23    userId: params.id,
24    message: `User ${params.id} retrieved successfully`
25  };
26});
27
28// Start the server
29await app.listen(() => {
30  const config = app.getConfig();
31  console.log(
32    `🚀 MoroJS running on http://localhost:${config.server.port}`
33  );
34});

💡 Tip: Parameters are automatically typed. TypeScript knows params.id is a string.

3

Run Your API

Start the development server:

Start Development Server

bash

1# Start development server with hot reload
2npm run dev

Test Your API

Your API is now running! Try these endpoints:

  • GET http://localhost:3000/
  • GET http://localhost:3000/users/123
4

Add Type-Safe Validation

Now let's add validation to ensure type safety:

src/server.ts - With Validation

typescript

1import { createApp } from '@morojs/moro';
2import { z } from 'zod';
3
4const app = createApp();
5
6// Define validation schemas
7const CreateUserSchema = z.object({
8  name: z.string().min(1),
9  email: z.string().email(),
10  age: z.number().min(18).max(120)
11});
12
13// POST route with body validation
14app.post('/users', {
15  body: CreateUserSchema,
16  handler: ({ body }) => {
17    // body is now type-safe and validated!
18    // body.name: string
19    // body.email: string
20    // body.age: number
21    return {
22      message: 'User created successfully',
23      user: {
24        id: crypto.randomUUID(),
25        ...body,
26        createdAt: new Date().toISOString()
27      }
28    };
29  }
30});

✨ Result: TypeScript automatically knows what properties are available. No type assertions needed!

Progress4 of 4 steps

Next Steps