HTTP
Orionjs comes with a http
module, powered by micro.
Routes location
Orionjs provides an example for routes management in the services/http
folder:
server
└── app
└── services
├── http
│ ├── home.js
│ └── index.js
└── index.js
## Defining a route
To define a new route you must call the route
function from the app module.
import {route} from '@orion-js/app'
route(path, func)
path
: A string that compilant with path-to-regexp. To setup a route that responds when no other route is found, setpath
tonull
.func
: The function that will be executed when the route is visited. It must be a function, the result of this function will be the response.
Function arguments:
params
: The parameters defined in the route path.query
: The options passed in the url query.pathname
: The final pathname.request
: HTTP request object.response
: HTTP response object.headers
: An object with the passed headers.getBody
: A function that returns a promise resolving the request body in text.viewer
: An object with the information about the current viewer.
CORS
You can define CORS
options to all routes by calling this function:
import {setCorsOptions} from '@orion-js/app'
setCorsOptions({
origin: '*'
})
By default, Orionjs provides this functionality in the index
file of the services/graphql
folder:
server
└── app
└── services
├── graphql
│ └── index.js
└── index.js
Configuration
The configuration of CORS
is done by setting the following variables:
maxAge
Default value: 86400origin
Default value: '*'allowHeaders
Default value: ['X-Requested-With', 'Access-Control-Allow-Origin', 'X-HTTP-Method-Override', 'Content-Type', 'Authorization', 'Accept']exposeHeaders
Default value: []allowMethods
Default value: ['POST', 'GET', 'PUT', 'PATCH', 'DELETE', 'OPTIONS']
Custom viewer
The authentication module handles the viewer object, but if you want to define a custom viewer getter, there's a function for that.
import {setGetViewer} from '@orion-js/app'
setGetViewer(func)
func
: A async function. The object that this function returns will be the viewer in resolvers and http routes. This function will recieve the same arguments that the route handler recieves.