H5P Ajax Endpoints
Last updated
Last updated
There are two ways of handling AJAX requests: You can use the out-of-the-box Express adapter or write your own custom router:
Your implementation must process requests to several endpoints and relay them to the H5PEditor or H5PPlayer objects. All Ajax endpoints are already implemented in the Express adapter, which you can use like this:
Import the Express adapter router like this:
or in classic JS style:
Then add the router to your Express app like this
Note that the Express adapter does not include pages to create, editor, view, list or delete content!
You can customize which endpoints you want to use by setting the respective flags in the options
object. By default, the adapter will handle all routes and you can turn individual ones off by setting routeXX
to false. You can also turn off the error handling (handleErrors: false
). Normally, the router will send back localized responses that the H5P client can understand. If you turn error handling off, the routes will throw errors that you have to handle yourself!
IMPORTANT: The adapter expects the requests object of Express to be extended like this:
The function t
must return the string for the errorId translated into the user's or the content's language. Replacements are added to the localized string with curly braces: It is suggested you use i18next for localization, but you can use any library, as long as you make sure the function t is added to the request object.
If you use a different HTTP framework than Express, you can write your own adapter. In this case, you must instantiate H5PAjaxEndpoint
and call its methods when your routes are called.
The table below shows which routes you must implement and which ones can be left out. Note that routes of the Type H5P are needed by the H5P client and must be implemented in some way. Routes of the type custom are specific to @lumieducation/h5p-server. The exact name of the routes can be configured in IH5PConfig and might be different in your setup.
HTTP Verb | Route | method in H5PAjaxEndpoint | Type | Required |
---|---|---|---|---|
GET | /ajax | getAjax | H5P | yes |
GET | /content | getContentFile | H5P | depends on content storage: files in FileContentStorage can also be served statically |
GET | /libraries | getLibraryFile | H5P | depends on library storage: files in FileLibraryStorage can also be served statically |
GET | /temp-files | getTemporaryFile | H5P | yes |
POST | /ajax | postAjax | H5P | yes |
GET | /params | getContentParameters | custom | if you use the default renderer script of the editor |
GET | /download | getDownload | custom | no |
Consult the documentation of H5PAjaxEndpoint
for details on who to retrieve the required parameters from the HTTP requests. You can also look at the Express Ajax Adapter as an example.
The H5P client (run in the browser by the user) can be configured to use custom AJAX request endpoints. These can be configured in the config object. The relevant settings (including defaults) are: