The much more fundamental OstroJS routes take a URI and a callback, allowing you to define routes and behaviour without having to deal with complex routing configuration files:
const route = require('@ostro/support/facades/route')
route.get('/greeting', function ({ response }) {
response.send('Hello World');
});
Your route files, which are stored in the routes directory, specify all OstroJS routes
. Your app's app/providers/routeServiceProvider
loads these files for you automatically. Routes for your web interface are defined in the routes/web.js
file. The web
middleware group is allocated to these routes, which includes features like session state and CSRF protection. The api
middleware group is given to the routes in routes/api.js
, which are stateless.
In your routes/web.js
file, you'll start by establishing routes for most apps. By inputting the defined route's URL in your browser, you may access the routes defined in routes/web.js
. By going to http://example.com/user
in your browser, for example, you may go to the following route:
route.get('/user', 'userController::index');
The RouteServiceProvider
nests routes declared in the routes/api.js
file within a route group. The /api
URI prefix is applied automatically inside this group, so you don't have to explicitly add it to each route in the file. Modify your RouteServiceProvider
class to change the prefix and other route group parameters.
You may use the router to create routes that respond to any HTTP verb:
route.get($uri, $callback);
route.post($uri, $callback);
route.put($uri, $callback);
route.patch($uri, $callback);
route.delete($uri, $callback);
route.options($uri, $callback);
It's possible that you'll need to register a route that responds to several HTTP verbs at some point. You can use the match
technique to do so. You can also use the any
method to register a route that listens to all HTTP verbs:
route.match(['get', 'post'], '/', function (httpContext) {
//
});
route.any('/', function (httpContext) {
//
});
Remember to add a CSRF token field in any HTML forms linking to the web routes file's POST
, PUT
, PATCH
, or DELETE
routes. The request will be denied if it is not submitted in this manner. In the CSRF documentation, you may learn more about CSRF security:
<form method="POST" action="/profile">
<input type="hidden" value="<%= helpers.csrf_token() %>" />
...
</form>
You may use the route.redirect
function to define a route that redirects to another URI. This technique provides a quick shortcut for doing a basic redirect without having to specify a full route or controller:
route.redirect('/here', '/there');
route.redirect
delivers a 302 status code by default. The optional third argument can be used to modify the status code:
route.redirect('/here', '/there', 301);
Or, you may use the route.permanentRedirect
method to return a 301 status code:
route.permanentRedirect('/here', '/there');
You may use the route.view
function if your route just needs to return a view. This technique, like the redirect method, offers a quick way to avoid having to create a whole route or controller. A URI is provided as the first parameter to the view method, and a view name is passed as the second. Additionally, as an optional third argument, you may send an array of data to the view:
route.view('/welcome', 'welcome');
route.view('/welcome', 'welcome', {'name' : 'Amar'});
Within your route, you may need to capture portions of the URI. For instance, you might need to extract a user's ID from a URL. You may do that by setting route parameters like this:
route.get('/user/:id', function ({ params, response }) {
response.send('User '+params.id);
});
You may specify as many route parameters as your route requires:
route.get('/posts/:post/comments/:comment', function ({ params, response }) {
response.json({post : params.post, comment : params.comment})
});
Route parameters should always be start with column and include alphabetic letters. Within route parameter names, underscores (_) are also permitted. The sequence in which route parameters are injected into route callbacks / controllers is irrelevant; the names of the route callback / controller arguments are irrelevant.