malta-restify

    1.1.6 • 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:

    • entrypoints (mandatory)
    • port [3001]
    • host [127.0.0.1]
    • folder [execution one, if given must be relative to it] (from v. 1.1.3)
    • delay, in millisecond to delay the response (from v. 1.1.6) [default 0]
    • handlers, the path (relative to execution) where one or more named handlers are exported
    • idTpl: a string that contains <uniq> that will be used to create the id value of new elements created using POST; default is ID_<a uniq number>

    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\",post: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):

    {
        "del": [
            {
                "ep": "/person/:id",
                "source": "./source/data/persons.json",
                "key": "id"
            }
        ],
        "post": [
            {
                "ep": "/persons",
                "source": "./source/data/persons.json"
            }
        ],
        "get": [
            {
                "ep": "/persons",
                "source": "source/data/persons.json"
            },
            {
                "ep": "/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 ti and pass the path as the handlers parameter to Malta.

    Now the only missin thing is to add the indicatioj in the restify.json file, for example:

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

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

    Install

    npm i malta-restify

    DownloadsWeekly Downloads

    3

    Version

    1.1.6

    License

    MIT

    Unpacked Size

    16.2 kB

    Total Files

    5

    Last publish

    Collaborators

    • fedeghe