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'
*/