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

rest-api-cli

v1.1.1

Published

A simple node REST API boilerplate

Readme

REST API cli

There are some patterns that I prefer when writing a REST API using node, and this module is just a simple cli that scaffolds out a basic example of those patterns.

Modules Used:

  • restify - for serving the RESTful endpoints
  • pg - for connecting to a PostgreSQL database
  • knex - for writing database-agnostic queries in js
  • morgan - for logging
  • lib - resuable pieces that I wanted to keep with the source, but could also be required without doing relative paths like require('../../../conn')

devDependencies:

  • dotenv - to keep config outside of the code
  • eslint - set up using 'standard' style
  • nodemon - to watch for file changes during development
  • pre-commit - commits fail when npm test fails
  • sqlite3 - used as the db during development

Getting Started

For development, you just need node v4 OR v6 and lastest npm, which you cant get by running npm i -g npm.

Creating the example project:

  • npm i -g rest-api-cli
  • rest-api-cli my-new-project
  • cd my-new-project
  • npm i
  • npm run db
  • npm start

You should be able to hit localhost:8079/people and get some results

Patterns

The entry point of the application is src/index.js, which sets up some restify plugins, logging, routes, and the server. A line of interest is:

queries(server)

The queries function is the export of the src/queries/index.js file. This file is where we pass out restify server into various files containing routes.

module.exports = app => {
  // require your queries here
  require('./people')(app)
  require('./places')(app)
}

So that takes us to our src/queries/people.js and src/queries/places.js, where the real action is happening.

The places file takes advantage of some defaults craeted for calling CRUD operations that require no relational joins. It uses the queries already written in lib/query.js.

All we have to do is export a function that creates the routes, and pass our queries as the callback.

const db = require('lib/db-conn')
const query = require('lib/query')

const table = 'places'
const columns = [
  'id',
  'city',
  'state',
  'country'
]

const places = query(db, table, columns)

module.exports = app => {
  app.get('/places', places.index)
  app.get('/places/:id', places.find)
  app.post('/places', places.create)
  app.put('/places/:id', places.update)
  app.del('/places/:id', places.destroy)
}

If some joins are needed, then just write the callback functions, like in src/queries/people.js:

const table = 'people'
const columns = [
  'p.id',
  'p.first_name',
  'p.last_name',
  'p.age',
  'pl.city',
  'pl.state',
  'pl.country'
]

const people = query(db, table, columns)

// Write our own queries for the GET methods,
// so we can add some joins
function index (req, res, next) {
  db(`${table} AS p`)
    .select(columns)
    .leftJoin('places AS pl', 'p.place_id', 'pl.id')
    .then(rows => res.send(rows))
    .then(() => next())
    .catch(query.error)
}

function find (req, res, next) {
  db(`${table} AS p`)
    .select(columns)
    .where('p.id', req.params.id)
    .leftJoin('places AS pl', 'p.place_id', 'pl.id')
    .then(rows => res.send(rows))
    .then(() => next())
    .catch(query.error)
}

module.exports = app => {
  app.get('/people', index)
  app.get('/people/:id', find)
  app.post('/people', people.create)
  app.put('/people/:id', people.update)
  app.del('/people/:id', people.destroy)
}

todo

  • add testing