Comment on page
Integrating the core library
Add the library to a project by executing
npm install @lumieducation/h5p-server
const H5P = require('@lumieducation/h5p-server');
and instantiate the editor with
const h5pEditor = H5P.fs(
await new H5P.H5PConfig(
path.resolve('examples/config.json') // the path on the local disc
// where the configuration file is stored
path.resolve('h5p/libraries'), // the path on the local disc where libraries
// should be stored
path.resolve('h5p/temporary-storage'), // the path on the local disc where
// temporary files (uploads) should
// be stored
path.resolve('h5p/content') // the path on the local disc where content is
This object is a server-side component that is long-lived and includes most of the methods that must be called when by the routes of your server. To find out what these routes are and how they must call H5PEditor, you can either check out the section on content views in the docs or the Express example project.
To render a HTML page (server-side rendering) with the editor inside (= the HTML user interface around the actual editor and the editor itself) for a specific content ID, you call
const html = await h5pEditor.render(contentId, user);
// send the html to the browser, which will display it
To use a custom renderer, change it with
h5pEditor.setRenderer(model => /** HTML string or object **/);
You can also use a custom renderer that returns a plain object with the data required to display an H5P editor instead of HTML markup. If you return this data to the browser as a response to an Ajax call, you can also create Single Page Applications that don't rely on server-side rendering.
The H5P client (running in the browser) sends many AJAX requests to the server (this application). While this library provides you with everything required to process the requests in the backend, your implementation must still serve the requests to these endpoints. There is an Express adapter that you can use out-of-the box for this purpose. Check out the documentation on endpoints for details.
You must add a route to your implementation that serves the static files found under
h5p/editorto the endpoint configured in
config.libraryUrl. The out-of-the-box Express adapter already includes a route for this.
While the AJAX communication between the actual H5P editor client (running in the browser) and the server (this application) can be fully handled by the Express adapter, you must still create custom views for these purposes:
- View that is shown when creating new content
- View that is shown when editing existing content
- View for deleting content
- View that lists existing content
- View that plays content
The reason why you have to do this on your own is that this library is unaware of other data that your system might attach to a piece of content (e.g. access rights, tags). If you want any custom UI elements around the editor and player (which is highly likely), you must put this into the views. Check out the example for how to write custom views.
Several aspects of your H5P server can be customized by creating your own implementation of interfaces and passing them to the constructor of
H5PEditor. That way you can use a database of your choice, cache data in Redis or store user data in an object storage system.
The interfaces that can be implemented are:
There are already default implementations that you can use:
- The implementations in the
fsfolder store all data in the local file system and are only for demonstration purposes and not suitable to be used in a multi-user environment and not optimized for speed. You might be able to use them in a cluster setup by using a network storage.
- There is an implementation of the library file storage for MongoDB and S3-compatible storage systems.
The implementation needs to call several function regularly (comparable to a cronjob):
H5PEditor.temporaryFileManager.cleanUp()every 5 minutes. This checks which temporary files have expired and deletes them if necessary. It is important to do this, as temporary files are not automatically deleted when a piece of content is saved.
H5PEditor.contentTypeCache.updateIfNecessary()every 12 hours. This will download information about the available content types from the H5P Hub. If you don't do this, users won't be shown new content types or updates to existing content types when they become available.
If something goes wrong and a call to the library can't continue execution, it will normally throw either a
AggregateH5PError(a collection of several errors). Both errors types represent errors that can be sent to the user to be displayed in the client (in the user's language). They don't include the English error message but an error id that you must translate yourself. Error ids and their English translations can be found in [
/packages/h5p-server/assets/translations]. The translation strings follow the format used by i18next, but in theory you can use any localization library.
Calls to the library might also throw regular
Errorobjects. In this case the error is not caused by the business logic, but by some more basic functionality (file system, other library) or it might be an error that is addressed at the developer (i.e. because function parameters aren't correctly used).
The Express adapter already catches errors, localizes them and returns proper HTTP status codes. Check out the implementation there for a guide how to deal with errors.
To conform with local law, you probably have to compile a privacy declaration for your application. You can check out the documentation page on privacy to find out what this library does with your users' personal data.