Skip to main content

overview

Psychic follows a paradigm set up by many other MVC frameworks, which is to have a centralized file from which all of your application's routes can be derived. Though some frameworks have done away with such mechanisms, favoring implicit routing built into the application's controllers, Psychic prefers the centralized routing system, since it gives the engineer a high-level overview of their application, and enables them to easily switch routes on and off.

The route bindings for a Psychic application can be found in the conf/routes.ts file. Routes are expressed elegantly using standard HTTP-verb driven language, like so:

import { PsychicRouter } from 'psychic'

export default (r: PsychicRouter) => {
r.get('/health-check')
}

GET     /health-check  HealthCheck#ping

RESTful routing

The RESTful verbs (GET, POST, PATCH, PUT, and DELETE) are the backbone of expressive routing in a web application.

Here is an example, demonstrating how these verbs might be used to clearly express a resourcefully-driven application:

import { PsychicRouter } from 'psychic'

export default (r: PsychicRouter) => {
r.post('places', PlacesController, 'create')
r.get('places', PlacesController, 'index')
r.get('places/:id', PlacesController, 'show')
r.patch('places/:id', PlacesController, 'update')
r.put('places/:id', PlacesController, 'update')
r.delete('places/:id', PlacesController, 'destroy')
}

POST     /places      Places#create
GET      /places      Places#index
GET      /places/:id  Places#show
PATCH    /places/:id  Places#update
PUT      /places/:id  Places#update
DELETE   /places/:id  Places#destroy

To learn more about REST-driven routing mechanisms, see our REST guide.

Resourceful routing

Since this type of routing pattern is quite common, Psychic provides encapsulated methods which will automatically build out these exact route directives in a single call, using the resources method, like so:

import { PsychicRouter } from 'psychic'

export default (r: PsychicRouter) => {
r.resources('places')
}

POST     /places      Places#create
GET      /places      Places#index
GET      /places/:id  Places#show
PATCH    /places/:id  Places#update
PUT      /places/:id  Places#update
DELETE   /places/:id  Places#destroy

To learn more about resourceful routing, see our resourceful routing guide.

Namespacing

The Psychic router also provides techniques for namespacing your application, using nested callbacks to organize clusters of routes together, like so:

import { PsychicRouter } from 'psychic'

export default (r: PsychicRouter) => {
r.namespace('v1', r => {
r.namespace('host', r => {
r.resources('places')
})
})
}

POST     /v1/host/places      V1/Host/Places#create
GET      /v1/host/places      V1/Host/Places#index
GET      /v1/host/places/:id  V1/Host/Places#show
PATCH    /v1/host/places/:id  V1/Host/Places#update
PUT      /v1/host/places/:id  V1/Host/Places#update
DELETE   /v1/host/places/:id  V1/Host/Places#destroy

To learn more about techniques for namespacing, see our namespacing guide.