malta-restify

1.1.11 • Public • Published

npm version npm downloads npm downloads

This plugin can be started on any file

> yarn add malta-restify

It starts a really raw simple http server, parameters:

  • endpoints
    grouped per HTTP verb the list of endpoints, the minimum setting for one endpoint follows:
    {
        "GET": [{
            "path": "/users", 
            "source": "data/users.json"
        }]
    }
    when for GET, DELETE, PATCH or PUT a parameter is required it becomes:
    {
        "DELETE": [{
            "path": "/users/:id", 
            "source": "data/users.json",
            "key": "id"
        }]
    }
    Notice: CONNECT, OPTIONS and TRACE are not supported
  • port: default is 3001 (u need to use root to start on ports < 1024)
  • host: default IP is 127.0.0.1
  • folder: the path relative to execution where files targeted in endpoints are reachable; default is malta execution folder.
  • delay: in millisecond to delay the response [default 0]
  • handlers: the path (relative to execution) where one or more named handlers are exported; default is malta execution folder
  • idTpl: a string that contains <uniq> that will be used to create the id value of new elements created using POST; default is ID_<uniq>
  • authorization: string - when specified will require every request to send this in an authorization header.
  • verbose: boolean - defaulted to true allows to shut up the after request notifications when false.

So for example if we want to start it automatically at (first) build, using public as webRoot, with a specific ip on a specific port; the entrypoints folder must be relative to folder, which if not specified is the execution one; all paths must be relative to th default or specified folder

> malta source/index.js public -plugins=malta-restify[endpoints:\"source/restify.json\",port:3452,delay:1e3]

or in the .json file :

{
    "source/index.js": ". -plugins=malta-restify[endpoints:'source/restify.json']"
}

the entrypoints have the following structure (in the example source/restify.json):

{
    "DELETE": [
        {
            "path": "/person/:id",
            "source": "./source/data/persons.json",
            "key": "id"
        }
    ],
    "POST": [
        {
            "path": "/persons",
            "source": "./source/data/persons.json"
        }
    ],
    "GET": [
        {
            "path": "/persons",
            "source": "source/data/persons.json"
        },
        {
            "path": "/person/:id",
            "source": "source/data/persons.json",
            "key": "id"
        }   
    ]
}

and persons.json could be something like:

[
    {
        "id": 1,
        "name": "Federico"
    },
    {
        "id": 2,
        "name": "Gabriele"
    }
]

where the source referenced file has to be relative to the current execution folder.

The only thing one needs to take care of when referencing a specific element for example in the case of the GET /person/:id is that here the key value must be present inside the persons.json file since will be used for retriving the specific object (or to delete it, check the first DEL rule)


Another small example

an additional option is available from version 1.0.6

  • authorization

when specified, every request must include a header surprisingly named authorization with the exact value passed to the plugin; in case it does not match will receive back a 401 http code (unauthorized).


Your handlers ( from v 1.1.3 )

Could be useful to be able to setup simple handlers to fulfill some requests; to do that, first create a file fo the special handlers, for example:

const users = require('./users.json')
const checkCredentials = ({
    req, res, verb, ep
}) => {
    res.send(202, users);
}

module.exports = {
    checkCredentials
}

then save it and pass the path as the handlers parameter to Malta.

Now the only missing thing is to add to one or more endpoints a handler string containing the function name of the needed handler, for example:

"GET": [
    {
        "path": "/checkcredentials",
        "handler": "checkCredentials", // the name must match
        "any": "other",
        "parameter": "here"
    },

path and handler are mandatory, while all others are optionals and will be passed to to the handler within the `endpoint`` parameter.

Package Sidebar

Install

npm i malta-restify

Weekly Downloads

2

Version

1.1.11

License

MIT

Unpacked Size

20 kB

Total Files

5

Last publish

Collaborators

  • fedeghe