npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

malta-restify

v1.1.11

Published

Malta plugin to start a development rest server.

Downloads

12

Readme


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.