Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

grand-central-junction

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

grand-central-junction

A simple, lightweight router for Node and Express

  • 0.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Grand Central Junction

A simple Rails-inspired router for Node built on top of Express.

Documentation

route(app, options)

Options that can be specified:

  • dir -- the home directory of the app. Defaults to the directory that contains the node_modules folder
  • routes -- the routes path, relative to the directory route() is called from. Defaults to config/routes
  • controllers -- the controllers path. Defaults to controllers

In your app.js file or wherever you initialize your Express app, add the following code:

var express = require('express'),
    gcj = require('grand-central-junction'),
    app = express();

gcj.route(app);

With custom options:

gcj.route(app, {
    dir: __dirname + '/app',
    routes: 'router/routes',    // Will get the routes from ./app/router/routes.js
    controllers: 'controllers'  // Will look for controllers in ./app/controllers/
});

Routes.js

Example of /config/routes.js:

var oauth = require('../oauth');

match('/',         'home#index');
match('/user/:id', 'user#get');
match('/user',     'user#create', {via: 'post'});
put('/user/:id',   'user#update');
del('/user/:id',   oauth.requireAuth, 'user#remove');

resources('/animal', 'animal');

Routes:

GET    /            => /controllers/home.js#index
GET    /user/:id    => /controllers/user.js#get
POST   /user        => /controllers/user.js#create
PUT    /user/:id    => /controllers/user.js#update
DELETE /user/:id    => /oauth.js#requireAuth >> /controllers/user.js#remove

GET    /animal      => /controllers/animal.js#index
GET    /animal/:id  => /controllers/animal.js#show
POST   /animal      => /controllers/animal.js#create
PUT    /animal/:id  => /controllers/animal.js#update
DELETE /animal/:id  => /controllers/animal.js#destroy
GET    /animal/create     => /controllers/animal.js#create
GET    /animal/edit/:id   => /controllers/animal.js#update
GET    /animal/delete/:id => /controllers/animal.js#destroy
match(route, action, [options])

A GET route to the specified controller/action.

  • route string regex-- the Express routing string/RegEx
  • action string function -- either a direct callback with req, res params or a string in the format of 'controller#action', with controller being the name of the file in the controllers directory and action being the name of the method in the controller.
  • [options] object
    • via changes the method of the route: get (default), post, put, delete
    • method same as via
VERB(route, [action...], action)

Same as match() options above, but without options and the ability to include many actions. VERB = get|post|put|del|all

resources(route, [controller])

Maps all HTTP methods to their respective CRUD actions in a controller. The action names are the same as those in Rails with a few extras added in (see above list of routes for names).

  • route string regex -- the Express routing string/RegEx
  • controller string -- the name of the file in the controllers directory, so 'project' would be the name of ./controllers/project.js. This defaults to whatever name is in the route string.

Keywords

FAQs

Package last updated on 14 Nov 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc