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

express-plumber

v1.0.2

Published

express framework utilities reducing boilerplate code

Downloads

5

Readme

express-plumber

Utility to

  • remove the burden of writing boilerplate code
  • speeding up development of express applications
  • separating concerns by splitting route and middleware into separate files

by rigorously imposing a convention to adhere to. It provides various functions and utitlies to facilitate rapid protoyping and development.


Implemented features

Planned features

  • Auto-reloading of routes on file change

Quickstart

  • Create the directories routes/GET
  • create the index.js file inside of it
  • paste the following skeleton into it:
const middlewares = require('express-plumber').loadMiddlewares()

module.exports = {
    middlewares: [
        // List of middlewares here
    ],

    // Ignore it for now, we'll get to it in a bit
    parametrizePath: false,

    // Ignore it for now
    priority: 0,

    // This is the callback that will be exectued by express
    // when / is being requested
    callback: async (request, response, next) => {

        try {
            response.json({message: `Hello from ${
                    request.originalUrl
                }`})
        } catch (exception) { // Todo: Implement error handling
            console.log(exception)
        }

    }
}

Open or create your index.js in the project root directory and add the following code

const express = require('express')
const plumber = require('express-plumber')

const app = express()

// Use json parser to service requests with content-type application/json
app.use(express.json())

// Invoke the loading and applying of routes to your app 
plumber.loadAndApplyRoutes(app)

app.listen(42000, () => {
  console.log(`Server started`)
})

Open your browser and navigate to http://localhost:42000 and you should receive

{
  "message": "Hello from /"
}

Adding middleware

Any middleware you write can be added to any route. Create a directory named middlewares in the project root directory and create a file named demo.js inside of it. Paste the following code:

module.exports = async function (request, response, next) {

    // We'll be adding a random double to the locals of 
    // the response object and grab it from the route for /
    response.locals.random = Math.random()

    // Invoke the next() method to process other middlewares
    next()
}

Now go back and modify routes/GET/index.js as follows:

const middlewares = require('express-plumber').loadMiddlewares()

module.exports = {
    middlewares: [
        // The demo middleware was loaded and is now available
        middlewares.demo,
    ],

    // Ignore it for now, we'll get to it in a bit, still
    parametrizePath: false,

    // Ignore it for now
    priority: 0,

    // This is the callback that will be exectued by express
    // when / is being requested
    callback: async (request, response, next) => {

        try {
            response.json({
                  message: `Hello from ${request.originalUrl}.`,
                  random: response.locals.random
            })
        } catch (exception) { // Todo: Implement error handling
            console.log(exception)
        }

    }
}

Refresh http://localhost:42000 and you should see a new random every refresh

{
  "message": "Hello from /",
  "random": 0.7689725270201255
}

Features

Convention over configuration is plumber's mantra. By sticking to a given convention we save a ton of time writing configuration centric code.

Route organization in directories and files

Routes are organized by request method: GET, POST, PUT, DELETE and PATCH are currently supported. plumber.loadAndApplyRoutes() by default tries to load routes from `cwd`/routes by traversing and enumerating recursively any directory it comes across.

Sample directory structure

Example routes

| Request | Source file path | Parsed route path | parametrizePath | | :- | :- | :- | :-: | | GET / | routes/GET/index.js | / |false | | GET /user | routes/GET/user/index.js | /user |false | | GET /post/1 | routes/GET/post/id.js | /post/:id |false | | POST /post | routes/POST/post/index.js | /post | false | | GET /sso/ZaGe5bQUaD/WCM9CvEg | routes/GET/sso/account/token.js | /:sso/:account/:token | true |


Route prioritization

Routes are loaded as they are being read from disk. This does not always match business logic such as the classical use case: the authentication endpoint. If you need a route to be declared before another route that matches the path, you can do so by raising the desired route's priority.

The higher the priority, the earlier it will be passed to the router or app

Example:

const middlewares = require('express-plumber').loadMiddlewares()

module.exports = {
    middlewares: [],

    // Ignore it for now
    priority: 9001, // >9000

    // Route callback
    callback: async (request, response, next) => {
    }
}

Route path override

If the convetion of path and parameter name does not meet the use case, you can override the path by setting it in the route file:

const middlewares = require('express-plumber').loadMiddlewares()

module.exports = {
    middlewares: [],

    // Ignore it for now
    path: '/meet/:me/:here/:at',

    // Route callback
    callback: async (request, response, next) => {
    }
}

Utility functions

Load some other dependency

const plumber = require('express-plumber')

const packageDotJson = plumber.require('@/package.json')
console.log(packageDotJson)

Result:

{
    name: 'plumber-express',
    version: '0.1.0',
    [...]
}