🎉 RamAPI v1.0 is now available! Read the Getting Started Guide
Documentation
Getting Started

Getting Started

Welcome to RamAPI! This guide will help you get up and running with RamAPI in just a few minutes.

Prerequisites

Before you begin, ensure you have the following installed:

  • Node.js: Version 18.0.0 or higher
  • npm: Version 9.0.0 or higher (or yarn/pnpm)

Installation

Install RamAPI via npm:

npm install ramapi

Or using yarn:

yarn add ramapi

Or using pnpm:

pnpm add ramapi

Your First RamAPI Application

Let's create a simple "Hello World" application.

1. Create a new file

Create a file named server.ts (or server.js if not using TypeScript):

import { RamAPI } from 'ramapi';
 
const app = new RamAPI();
 
// Define a route
app.get('/', (req, res) => {
  res.json({
    message: 'Hello from RamAPI!',
    timestamp: new Date().toISOString()
  });
});
 
// Start the server
const PORT = process.env.PORT || 3000;
 
app.listen(PORT, () => {
  console.log(`🚀 RamAPI server running on http://localhost:${PORT}`);
});

2. Run your application

node server.js

Or with TypeScript:

ts-node server.ts

3. Test your endpoint

Open your browser and navigate to http://localhost:3000, or use curl:

curl http://localhost:3000

You should see:

{
  "message": "Hello from RamAPI!",
  "timestamp": "2024-01-01T00:00:00.000Z"
}

Adding More Routes

Let's add a few more routes to demonstrate RamAPI's routing capabilities:

// GET request
app.get('/users', (req, res) => {
  res.json({ users: ['Alice', 'Bob', 'Charlie'] });
});
 
// POST request
app.post('/users', (req, res) => {
  const { name } = req.body;
  res.json({
    message: 'User created',
    user: { name, id: Date.now() }
  });
});
 
// Route with parameters
app.get('/users/:id', (req, res) => {
  const { id } = req.params;
  res.json({
    user: { id, name: 'John Doe' }
  });
});
 
// DELETE request
app.delete('/users/:id', (req, res) => {
  const { id } = req.params;
  res.json({
    message: 'User deleted',
    userId: id
  });
});

Using Middleware

RamAPI supports middleware for handling cross-cutting concerns:

// Logger middleware
app.use((req, res, next) => {
  console.log(`${req.method} ${req.path}`);
  next();
});
 
// Authentication middleware
app.use('/api/*', (req, res, next) => {
  const token = req.headers.authorization;
 
  if (!token) {
    return res.status(401).json({ error: 'Unauthorized' });
  }
 
  // Verify token...
  next();
});

Error Handling

RamAPI provides built-in error handling:

// Custom error handler
app.use((err, req, res, next) => {
  console.error('Error:', err);
  res.status(500).json({
    error: 'Internal Server Error',
    message: err.message
  });
});

Next Steps

Now that you have a basic RamAPI application running, explore these topics:

Need Help?