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

am-merver

v0.0.13

Published

Micro Web Server for Micro Services

Downloads

5

Readme

Merver

Merver is a Micro HTTP Server for JSON apis and Microservices or even as stepping stone to building more robust backend solutions.

Merver is made up of three main classes to give it a modular approach, Merver, Middler, and Responder.

SPIN UP A TEMPLATE PROJECT: npx merced-spinup merver projectName

Merver - This is your web server that listens for requests

Middler - each middler object allows you to create a chain of middleware which can be attached to Mervers to used before every request or attached to response objects created by a Responder to be run before the controller functions of that particular endpoint.

Responder - An object that can create and maintains potential request responses which is attached to a Merver at construction.

Merver

The merver object creates an http server, its constructor can be passed a config option that configures how the server works...

const app = new Merver({
  PORT: 4001,
  responder,
  middler: middleWare,
});

app.listen();

PORT: The Port number to listen on

responder: A responder object which carries all the routes for this application

middler: A middler object which has the global middleware registered to it, will be run before the responder on every request

allowOrigin: value of "Access-Control-Allow-Origin" header, defaults to "_"

requestMethod: value of "Access-Control-Request-Method" header, defaults to "_"

**allowMethods:**value of "Access-Control-Allow-Methods" header, defaults to "OPTIONS, GET"

**allowHeaders:**value of "Access-Control-Allow-Headers" header, defaults to "*"

**mwTimeout:**Amount of time in milliseconds before middleware timesout, useful if a lot of async processes in your middleware you want completed before responses. Defaults to 10 milliseconds

**resTimeout:**Amount of time in milliseconds before routes timeout, useful if a lot of your routes complete async tasks like db call, defaults to 200 milliseconds.

  • When both timeouts occur the request will get a default "no response"

serveState: boolean that determines whether files will be served statically or not

publicFolder: property that says which folder to serve as a static folder, defaults to "./public"

cache: property that determines how long files serve should be cached for

methods

instance.listen(callback) - Starts the server listener, will run a callback after starting listener if one is passed.

Responder

const responder = new Responder();

responder.newResponse({
  endpoint: "/",
  GET: (req, res) => {
    console.log(res.query);
    return res.html(`<h1>Hello World</h1>`);
  },
  // middler: middleWare2
});

responder.newResponse({
  endpoint: "/cheese/:param",
  GET: (req, res) => {
    console.log(res.query);
    return res.json({ Hello: "world" });
  },
  // middler: middleWare2
});

be careful with params as they may capture reference to static files, for example the route "/:param" will end up prioritized over "/index.html"

The Responder Object registers routes and their responses. There is one method...

responder.newResponse(config): Pass a new route object which has the following properties...

  • endpoint: the url string to match against (URL PARAMS CURRENTLY NOT SUPPORTED)
  • [VERB]: a function to be run for a request to the endpoint of that particular verb, format of the function is (request, response) => {}
  • middler: a middler object to run middleware only for this route

Middler

Each middler object registers its own unique of middleware functions which can be bound globally to a Merver object or to a specific responder route.

const middleWare = new Middler();

middleWare.addMiddleware((req, res) =>
  console.log(`${req.method} request to ${req.url}`)
);

const middleWare2 = new Middler();

middleWare2.addMiddleware((req, res) => console.log("I am middleware 2!"));

built-in middleware

middler.bodyparser - can handle json and urlencoded data

middleware.addMiddleware(middleware.bodyparser);

Other

res.json(response) => method of the response object that sends a JSON response

res.html(response) => method the response object that sends response as html string

req.query => object of all URL query keys and values

req.params => object with any url params which are denoted using "/:param/" in your routes.

Running Merver via HTTPS

Merver.init() the server callback function seperated from the http object in case you'd like to pass it to an http server. Here you go!

const Merver = require('merver)
const fs = require('fs')
const https = require('https')
const config = require('./config.js')
const app = new Merver(config)

https.createServer({
  key: fs.readFileSync('server.key'),
  cert: fs.readFileSync('server.cert')
}, (req, res) => app.init(req, res, app))
.listen(3000, function () {
  console.log('Example app listening on port 3000! Go to https://localhost:3000/')
})