Routing
Creating Routes
# Basic Routes
import { Controller, Route } from 'kasky';
@Controller()
class MyFirstController {
@Route.Post('/api/blog')
createBlog(req, res) {
res.created('Route to create blog');
}
@Route.Get('/api/blogs')
getAllBlogs(req, res) {
res.success('Route to get all blogs');
}
}
export default MyFirstController;
When a request is made to a route, the method that handles that request will receive two arguments. The first argument is a request object which contains methods to get information about the request. The second argument is a response object which contains methods to send a response to the client.
# Parent Routes
You can create a parent route by setting the baseRoute
property for the Controller using the @Controller()
decorator as seen below. See more information on the @Controller()
decorator here.
import { Controller, Route } from 'kasky';
@Controller({
baseRoute: 'api'
})
class MyFirstController {
@Route.Post('blog')
createBlog(req, res) {
res.created('Route to create blog');
}
@Route.Get('blogs')
getAllBlogs(req, res) {
res.success('Route to get all blogs');
}
}
export default MyFirstController;
In the example above, POST /api/blog
will resolve to the createBlog()
method and GET /api/blogs
will resolve to the getAllBlogs()
method of the controller class.
Route Paths
View reference here