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-restful-routes

v0.0.5

Published

Helper library to generate Ruby on Rails like RESTful routing for Express.

Downloads

12

Readme

Express RESTful Routes

Helper library to generate Rails like RESTful routing for Express.

Motivation

It'll tend to be very difficult to read and grasp routes if there are not rules about how to define routes, especially in the big application. This library provides Rails like RESTful route definition rules.

Quoted from http://guides.rubyonrails.org/routing.html.

Getting Started

Install

This library is a Node.js module available through the npm registry. Installation is done using the npm install or yarn add command:

npm install --save express-restful-routes
or
yarn add express-restful-routes

Define RESTFul Routes Example

Minimal Example

const express = require("express");
const app = express();
const expressRestfulRoutes = require('express-restful-routes')

const handler = (req, res, next) => {
  res.send("hello");
};

const routes = {
  users: [
    { index: handler },
    { show: handler },
    { new: handler ,
    { create: handler },
    { edit: handler },
    { update: handler },
    { destroy: handler }
  ]
};

app.use(expressRestfulRoutes.defRoutes(routes));

app.listen(5555, () => {
  console.log("server running... port 5555");
});

Example

const express = require("express");
const app = express();
const expressRestfulRoutes = require('express-restful-routes')

/* Middleware1 */
const middleware1 = (req, res, next) => {
  console.log("middleware1");
  next();
};

/* Middleware2 */
const middleware2 = (req, res, next) => {
  console.log("middleware2");
  next();
};

const handler = (req, res, next) => {
  console.log(req.app.locals.expressRestfulRoutes);
  /* Path helper functions will be available via app.locals.expressRestfulRoutes */

  if (req.params) {
    console.log(req.params);
  }

  res.send("hello");
};

const routes = {
  users: [
    { index: [middleware1, middleware2, handler] },
    /* will define app.get('/users', [middleware1, middleware2, handler]) */

    { show: [middleware1, middleware2, handler] },
    /* will define app.get('/users/:param', [middleware1, middleware2, handler])  */

    { new: [middleware2, handler] },
    /* will define app.get('/users/new', [middleware2, handler]) */

    { create: handler },
    /* will define app.post('/users', handler) */

    { edit: handler },
    /* will define app.get('/users/:param/edit', handler) */

    { update: handler },
    /* will define app.patch('/users/:param', handler) */

    { destroy: handler }
    /* will define app.delete('/users/:param', handler) */
  ],
  /* Only Specified Routes */
  books: [
    { index: handler },
    { new: handler },
    { create: handler },
    { edit: handler }
  ],
  /* Shorthand */
  comments: [
    { i: handler },
    { s: handler },
    { n: handler },
    { e: handler },
    { c: handler },
    { u: handler },
    { d: handler }
  ]
};

app.use(expressRestfulRoutes.defRoutes(routes));

app.listen(5555, () => {
  console.log("server running... port 5555");
});

By generating routes with defineRoutes(), path helper functions will be available in app.locals.expressRestfulRoutes. For instance:

const routes = {
  users: [
     { index: [middleware1, middleware2, handler] },
     /* usersIndexPath() will be added into the app.locals.expressRestfulRoutes. */

     { show: [middleware1, middleware2, handler] },
     /* usersShowPath(param: string) will be added into the app.locals.expressRestfulRoutes. */

     { new: middleware2, handler },
     /* usersNewPath() will be added into the app.locals.expressRestfulRoutes. */

     { create: handler },
     /* usersCreatePath() will be added into the app.locals.expressRestfulRoutes. */

     { edit: handler },
     /* usersEditPath(param: string) will be added into the app.locals.expressRestfulRoutes. */

     { update: handler },
     /* usersUpdatePath(param: string) will be added into the app.locals.expressRestfulRoutes. */

     { destroy: handler }
     /* usersDestroyPath(param: string) will be added into the app.locals.expressRestfulRoutes. */
  ]
}

...

app.use(expressRestfulRoutes.defRoutes(routes));

/*
the code above will add the following path helper functions to app.locals.expressRestfulRoutes.
These helpers can be called via app.locals.expressRestfulRoutes.

* usersIndexPath() => '/users'      => '/users'
* usersShowPath(param: string)      => '/users/:param'
* usersNewPath() => '/users'        => '/users/new'
* usersCreatePath() => '/users'     => '/users'
* usersEditPath(param: string)      => '/users/:param/edit'
* usersUpdatePath(param: string)    => '/users/:param'
* usersDestroyPath(param: string)   => '/users/:param'
*/