Quick Start

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

Prerequisites

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

1. Installation

npm

bash

1npm install @morojs/moro

yarn

bash

1yarn add @morojs/moro

pnpm

bash

1pnpm add @morojs/moro

Create Your Project

bash

1# Create a new project
2mkdir my-moro-api
3cd my-moro-api
4
5# Initialize package.json
6npm init -y
7
8# Install MoroJS
9npm install @morojs/moro
10
11# Install TypeScript
12npm install -D typescript @types/node
13
14# Create tsconfig.json
15npx tsc --init

2. Your First API

src/server.ts

typescript

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

package.json scripts

json

1{
2  "scripts": {
3    "dev": "tsx watch src/server.ts",
4    "build": "tsc",
5    "start": "node dist/server.js"
6  },
7  "devDependencies": {
8    "tsx": "^4.0.0"
9  }
10}

3. Run Your API

Start Development Server

bash

1# Install tsx for development
2npm install -D tsx
3
4# Start development server with hot reload
5npm run dev

Test Your API

Your API is now running! Try these endpoints:

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

4. Adding Type-Safe Validation

MoroJS includes built-in Zod validation for type-safe request handling:

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
13const UserParamsSchema = z.object({
14  id: z.string().uuid()
15});
16
17// GET route with param validation
18app.get('/users/:id', {
19  params: UserParamsSchema,
20  handler: ({ params }) => {
21    // params.id is now type-safe and validated
22    return {
23      userId: params.id,
24      message: `User ${params.id} retrieved successfully`
25    };
26  }
27});
28
29// POST route with body validation
30app.post('/users', {
31  body: CreateUserSchema,
32  handler: ({ body }) => {
33    // body is now type-safe and validated
34    return {
35      message: 'User created successfully',
36      user: {
37        id: crypto.randomUUID(),
38        ...body,
39        createdAt: new Date().toISOString()
40      }
41    };
42  }
43});
44
45app.listen(3000);

Next Steps

Performance Tip

Your API is already optimized for production! MoroJS delivers 68k+ req/sec out of the box. For production deployment, make sure to:

  • Set NODE_ENV=production
  • Enable compression middleware
  • Configure appropriate logging levels