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

autoroute-express-promise

v0.2.3

Published

autoroute for use with promises

Downloads

24

Readme

Auto-route Express Promise

Summary

Auto-route Express Promise routing made simple with a single place to update.

Installation

Install with npm:

npm install --save autoroute-express-promise

Project Status

  • Beta
  • Active (June 26, 2015)

Example

Route Set Up

import {routes} from 'autoroute-express-promise'
import isUserAuthenticated = require('../controllers/auth/auth')
import express = require('express')
import withMessageAs = require('../utilities/response-structure')

var router = express.Router()

var authenticatedRoute = route => router.route(route).all(isUserAuthenticated)

routes({
        baseRoute: authenticatedRoute,
        response: (response, result) => response.send(withMessageAs(result)),
        message: (o) => {console.log(o.routeName, o.methodName)}
    }, ['./controllers/**/index.js'])

module.exports = router

Route/Controller Set Up

File Structure:

Note, that this is how I do my file structure. Any file structure is OK.

├── controllers/
    ├── api1/
    │   └── index.js
    ├── api2/
    │   └── index.js
    └── api3/
        └── index.js

Code Structure:

Note that you can do an array of routes also.

import {method} from 'autoroute-express-promise'
import {getPet, petDied} = require('../../pets')

const PETS = '/pets',
      ID = 'petId',
      PET = PETS + '/:' + ID

var routes: AutoRouteExpressPromise.RouteDefinition = {
    route: PET,
    methods: [
        [ method.get,       (req: Request) => getPet(req.params[ID]) ],
        [ method.delete,    (req: Request) => petDied(req.params[ID]) ] ]
}

export = routes

Cleaner nested route syntax (version 0.2):

var routes = {
    '/pets': {
        _methods: [
            [ method.post,      (req: Request) => getPet(req.body) ] ]
        ':id': [
            [ method.get,       (req: Request) => getPet(req.params[ID]) ],
            [ method.delete,    (req: Request) => petDied(req.params[ID]) ] ]
    },
    '/cats': {
        ':id': [
            [ method.get,       (req: Request) => getCat(req.params[ID]) ] ]
        '/hamsters': { // Overrides cats because of the forward slash at beginning
            '/fish': [ // Overrides hamsters (hamsters bite)
                [ method.get,   (req: Request) => getFish(req.params[ID])] ]
        }
    }
}

Example with two routes (this is the old way, you can still do it this way.):

import {method} from 'autoroute-express-promise'
import {getPet, newPet, petDied} = require('../../pets')

const PETS = '/pets',
      ID = 'petId',
      PET = PETS + '/:' + ID

var routes: AutoRouteExpressPromise.RouteDefinition[] = [
    { route: PETS,
      methods: [
          [ method.post,       (req: Request) => getPet(req.body) ] ]
    },
    { route: PET,
      methods: [
          [ method.get,        (req: Request) => getPet(req.params[ID]) ],
          [ method.delete,     (req: Request) => petDied(req.params[ID]) ] ]
    }
]

export = routes

API

import {routes, method} from 'autoroute-express-promise'

routes(options, glob[]):

where options:

{
    baseRoute: (routeName: string) => any
    message?: (options: {routeName: string; methodName: string}) => void
    response: (client: Express.Response, result: any) => any
}

baseRoute: (Required) Uses route name (e.g., /api/myroute/:id) and return an express.js Router. E.g.,

var router = express.Router
var authenticatedRoute = route => router.route(route).all(isUserAuthenticated)

message: (Optional) A function which passes the route name and method name (post, get, etc). Used for writing logs when route is called by server, e.g.,

o => {console.log(o.routeName, o.methodName)}

where: o is {routeName: '/api/myroute/:id', methodName: 'get'}.

response: (Required) Used to wrap the result in some wrapper and send method, e.g.,

(client, result) => client.send({result: result})

where glob:

List of globs as specified in require-glob. E.g., ['./controllers/**/index.js'].

method:

This is an enumeration of all the express.js methods:

get, post, put, head, delete, options, trace, copy, lock, mkcol, move, purge, propfind, proppatch, unlock, report, mkactivity, checkout, merge, "m-search", notify, subscribe, unsubscribe, patch, search, connect

E.g.,

method.get // => 0
method[0] // => "get"