Basic Routing

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');
});

The Default Route Files

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.

Available Router Methods

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) {
   //
});

CSRF Protection

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>

Redirect Routes

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');

View Routes

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'});

Route Parameters

Required Parameters

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.

Route Groups

Without having to describe such qualities on each individual route, route groups let you share route features like middleware across a lot of different routes.

Nested groups try to "merge" qualities with their parent group in an intelligent way. While names and prefixes are added, middleware and where conditions are combined. Where necessary, namespace delimiters and slashes are automatically inserted to URI prefixes.

Middleware

You may use the middleware method before declaring the group to add middleware to each route inside it. The middleware are performed in the array in the following order:

route.middleware(['first', 'second']).group(function (route) {
    route.get('/', function () {
        // Uses first & second middleware...
    });
 
    route.get('/user/profile', function () {
        // Uses first & second middleware...
    });
});

Subdomain Routing

Subdomain routing can also be managed using route groups. Similar to route URIs, subdomains may be given route parameters, allowing you to use a piece of the subdomain in your route or controller. Before establishing the group, the domain method may be used to provide the subdomain:

route.domain('{account}.example.com').group(function (route) {
   route.get('user/{id}', function ({params}) {
       //
   });
});

Route Prefixes

Each route in the group may be prefixed with a specific URI using the prefix method. For example, you may wish to prefix all route URIs within the group with admin:

route.prefix('admin'),group(function (route) {
    route.get('/users', function () {
        // Matches The "/admin/users" URL
    });
});

Route Name Prefixes

Each route name in the group may have a provided string prefixed to it using the name method. For instance, you could wish to start the names of every route in the group with admin. We will make sure to include the trailing since the supplied text is prefixed to the route name precisely as it is requested. an element of the prefix:

route.name('admin.').group(function (route) {
    route.get('/users', function () {
        // Route assigned name "admin.users"...
    }).name('users');
});

Route Group Object

The group accepts objects that have the ability to write a name, suffix, namespace, middleware, and domain for integration in a single group:

route.group({domain: 'admin.domain.com', namespace:  '/admin', prefix: 'users', suffix: 'update', middleware: ['admin'], name: 'admin.' }, function(route) {
    route.get('profile',function(){
		 // Route assigned name "admin.users.profile.update" Matches The "admin.domain.com/users/profile/update" URL and 
    }).name('profile.update')
})