Skip to content

ivanhuay/hiroki

Repository files navigation

Hiroki

NPM version CircleCI

Hiroki helps you build REST APIs faster than ever using open source tools and standards that you and your team already know.

Table of Contents

Documentation

For detailed information, please read our full documentation.

Getting Started

Installation

npm install --save hiroki

Quick Start

Here's a basic example of how to create a simple REST API with Hiroki:

const express = require('express');
const hiroki = require('hiroki');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const app = express();

// Model definition
const UsersSchema = new mongoose.Schema({name: String});
const UsersModel = mongoose.model('Users', UsersSchema);

// Importing model
hiroki.importModel(UsersModel);

// Bodyparser middleware
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

// API route to pass data to Hiroki
app.use('/api/*', async (req, res) => {
    const path = req.originalUrl;
    const resp = await hiroki.process(path, {
        method: req.method,
        body: req.body
    });
    res.status(resp.status || 200).json(resp);
});

app.listen(8012, () => console.log('Server running on port 8012'));

Note: This example assumes you have already set up your MongoDB connection.

Configuration

You can customize Hiroki's configuration, such as changing the base path:

hiroki.setConfig({ 
    basePath: '/api/v2' // default is '/api'
});

Changelog

v2.0.0

  • Hiroki is now backend-agnostic. Express has been removed as a dependency.
  • Mongoose version updated.
  • The 'share' feature has been removed. Please check if this impacts your use case.

Full Changelog