How to add routes
First we create a new folder called routes
in the src
directory. And make a new file hello-world
- CommonJS
- ESM
- TypeScript
const { methods, Route } = require('@sapphire/plugin-api');
class UserRoute extends Route {
constructor(context, options) {
super(context, {
...options,
route: 'hello-world'
});
}
[methods.GET](_request, response) {
response.json({ message: 'Hello World' });
}
[methods.POST](_request, response) {
response.json({ message: 'Hello World' });
}
}
module.exports = {
UserRoute
};
import { methods, Route } from '@sapphire/plugin-api';
export class UserRoute extends Route {
constructor(context, options) {
super(context, {
...options,
route: 'hello-world'
});
}
[methods.GET](_request, response) {
response.json({ message: 'Hello World' });
}
[methods.POST](_request, response) {
response.json({ message: 'Hello World' });
}
}
import { methods, Route, type ApiRequest, type ApiResponse } from '@sapphire/plugin-api';
export class UserRoute extends Route {
public constructor(context: Route.LoaderContext, options: Route.Options) {
super(context, {
...options,
route: 'hello-world'
});
}
public [methods.GET](_request: ApiRequest, response: ApiResponse) {
response.json({ message: 'Hello World' });
}
public [methods.POST](_request: ApiRequest, response: ApiResponse) {
response.json({ message: 'Hello World' });
}
}
A quick glance on what we did above:
- First we extended the
Route
class to make our route - Second we specified the route name in the constructor
- Third we defined the GET method
- Finally we responded to the request with a message which we would be able to use later when we fetch the route