express-rest-builder

Simply build REST API with Express.js!

Usage no npm install needed!

<script type="module">
  import expressRestBuilder from 'https://cdn.skypack.dev/express-rest-builder';
</script>

README

Express REST Builder

Build REST API using Express.js with ease!

Introduction

This package was inspired by Laravel's resource controllers which simplifies creating REST services a lot. Same minimalistic approach was applied in this package to include only the most commonly used HTTP methods and abstract their logic from the developer. Write less to get more!

Example

index.js

var express = require("express")
var app = require("./express-rest-builder")(express())

app.listen(8080, function() {
    console.log("Listening on port 8080...")
})

app.restResource("posts", {
    getAll: (request, response) => response.json([{post: "here's a post yo"}, {post: "here's another post yo"}]).end(),
    getOne: (request, response) => response.json({post: "here's a post yo", id: request.params.posts}).end(),

    post: (request, response) => response.json({post: "saved that post yo"}).end(),
    put: (request, response) => response.json({post: "edited this post yo", id: request.params.posts}).end(),
    delete: (request, response) => response.json({post: "deleted this post yo", id: request.params.posts}).end()
})

app.restResource("posts.comments", {
    getAll: (request, response) => response.json([{comment: "here's a comment yo", post_id: request.params.posts}, {comment: "here's another comment yo", post_id: request.params.posts}]).end(),

    post: (request, response) => response.json({post: "commented on that post yo", post_id: request.params.posts}).end(),
    delete: (request, response) => response.json({post: "deleted this comment yo", id: request.params.comments}).end()
})

Installation

Two steps:

  1. npm i express-rest-builder
  2. Done.

API

Express REST Builder extends the app object of the Express.js. Therefore, to initialize it you have to require it and pass the app as a parameter:

var express = require("express")
var app = require("./express-rest-builder")(express())

Now you have a restResource function available on the app object.

app.restResource(resource, options)

resource string Required

The string describing the resource, will be parsed into a URL. For example, "posts" will become /posts/:posts route with optional parameter depending on the HTTP method. For nested resources separate them with a dot, so "posts.comments" will become /posts/:posts/comments/:comments route with :comments being an optional parameter depending on the method.

Note: route parameters always have the exact same name as the resource they're associated with.

options object Required

The object mapping the operations (HTTP methods) to the resolvers. All the available operations with the routes they create are described in the table below.

Name URL Method
getAll /posts GET
post /posts POST
getOne /posts/:posts GET
put /posts/:posts PUT
delete /posts/:posts DELETE

License

Apache License 2.0