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 🙏

© 2026 – Pkg Stats / Ryan Hefner

expressive-api

v1.2.1

Published

Express API serving that just makes sense

Downloads

41

Readme

Express API serving that just makes sense

You think of an API like a directory structure, so it's time you served it out of one.

Define your api like this:

|-apiDir
  |-user
     |-fetch.js
     |-delete.js
     |-create.js
  |-deep
     |-deeper
        |-explore.js
  |-mcHammer
     |-touch.js

Each endpoint must export a function which accepts requests and produces json-friendly output, like this:

explore.js

module.exports = function explore(req, callback) {
  var depth = req.body.depth;
  if (depth > 1000) {
    return {
      balrog: true
    };
  } else {
    return {
      balrog: false
    }
  }
}

For async functions, either works:

  • return a promise
  • call the callback param supplied to your endpoint

Here's how to actually serve the API:

var express = require('express');
var app = express();

// you'll want to be able to parse json and querystrings
var bodyParser = require('body-parser'); 
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());

// serve away
var serveApi = require('expressive-api');
app.use(serveApi('./apiDir'));

app.listen(3000, function () {
  console.log('And it's running - easy hey?');
});

Now you can hit your API:

GET /user/fetch
POST /deep/deeper/explore

It's worth mentioning again that this is a JSON only API.

Extra Features

Controlling which API files are included

Under the hood, this uses require-directory, so you can pass in any of its options directly like this:

app.use(serveApi('./apiDir'), {
  whitelist: /\.js$/
});

Define endpoints with whatever params you want

You can specify a transformParams(req, callback, res) function which gets applied to the usual parameters. The result should be an array of the parameters you want your API endpoints to receive.

Here's one I use:

function transformParams(req, callback, res) {
  // combine body and query into the params for the api
  var params = Object.assign({}, req.body);
  params = Object.assign(params, req.query);
  return [params, req.user, callback];
}

app.use(serveApi('./apiDir'), {
  transformParams: transformParams
});

Restricting endpoints to certain HTTP methods

Just add an array of allowedMethods as a property of your endpoint function, like this:

function saveData(req, callback){
  // do the saving
}

saveData.allowedMethods = ['POST']

module.exports = saveData;

Responding in whatever way you choose

Ordinarily, whatever object an endpoint's exported function returns, is sent to the client.

However, since res is passed as an argument too, you can actually respond however you like, overriding the default JSON response.

Your feature suggestions and pull requests are welcome on the repo.

PS: Dear react-redux-universal-hot-example, your sick boilerplate inspired some of what's baked into this. Thank you!