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

bubble-serv

v0.2.2

Published

Express middleware for fast prototyping JSON server (JSON-based REST api). Mainly intended for mock servers, tests, prototypes.

Downloads

1

Readme

bubble-serv

Express middleware for fast prototyping JSON server (JSON-based REST api). Mainly intended for mock servers, tests, prototypes.

Just put authorized.json file into user/ folder, and you will get user/authorized method in your API, wich returns content of file. Add authorized.post.json for separate response on POST request. You can use .js instead of .json for more complex response, in case you want to take into consideration query params, body content or path params.

Installation

npm install bubble-serv

Usage

var express = require('express');
var app = express();
var bodyParser = require('body-parser'); // is not a part of this bundle
var bubbleServ = require("bubble-serv");

// if body params will be used
app.use(bodyParser.json());

app.use( '/api', bubbleServ({apiRoot: "api-files",}) );

// start server
app.listen(3000);

Put file info.json into api-files/ folder. Open with browser URI: localhost:3000/api/info - you will get content of info.json.

POST, PUT, etc

Add prefix to file extension with http method name in lower case, and file will be served only for this http method. info.post.json will be served only to POST localhost:3000/info. Files without http-method prefix are used to any kind of http methods.

Resolving file path

When bubble-serv gets request POST user/info it try to find file by following sequence (similar to node "require" algorithm):

first with ".post" prefix by standard "node" sequence

  1. user/info/index.post.js first with ".post" prefix
  2. user/info/index.post.json .js has priority to .json
  3. user/info.post.js
  4. user/info.post.json

then without prefix

  1. user/info/index.js the same, but without ".post" prefix
  2. user/info/index.json
  3. user/info.js
  4. user/info.json

Bubbling

If nothing is found it bubbles up in folder tree. It creates path param "info" and goes up to folder user/, trying to find user/index.post.js with regular algorithm, etc.

Thus, if we have one file index.js in folder user, all request like user/id/some-params/and-more will be delegated to this user/index.js. Bubble-serv will generates pathParams for it: ["id", "some-params", "and-more"] during bubbling.

Using .js file to handle params. Context

To handle request parameters you have to export callback function from your .js file, for example user/index.js:

module.exports = function(context, request, response){
    return {...context}; // response content, sent to browser
}

request and response are regular express request/response objects. In request object bubbleServ property contains context data, which is used as a first argument for callback.

Context

Context is an object with data, useful to handle request:

Some additional data:

Options

Sample API

How to create sample API is described in sample.md

CORS (net::ERR_CONNECTION_REFUSED)

With Javascript in browser, when your script tries to fetch API from another domain, you can get connection error. This situation is called Cross-Origin Resource Sharing (CORS). Server have to send specific http headers to allow browser to read API. The best way is to use additional express middleware, such as cors.