-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
61 lines (51 loc) · 1.85 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const fs = require('fs');
const path = require('path');
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const express = require('express');
const server = express();
const H5P = require('../src');
const examples = require('./examples.json');
server.use('/favicon.ico', express.static(`${__dirname}/favicon.ico`));
server.use('/h5p/core', express.static(`${__dirname}/core`));
server.get('/', (req, res) => {
res.append('Content-Type', 'text/html');
res.end(
examples
.map(
(example, i) => `
<a href="examples/${i}">${example.library}: ${example.name}</a>
| <a href="${example.page}">original</a>`
)
.join('<br>')
);
});
server.get('/examples/:key', (req, res) => {
let key = req.params.key;
let name = path.basename(examples[key].h5p);
let dir = `${__dirname}/contents/${name}`;
server.use('/h5p/libraries', express.static(dir));
server.use(`/h5p/content/${name}`, express.static(`${dir}/content`));
let first = Promise.resolve();
if (!fs.existsSync(dir)) {
first = exec(`examples/download-example.sh ${examples[key].h5p}`);
}
const libraryLoader = (lib, maj, min) =>
require(`./contents/${name}/${lib}-${maj}.${min}/library.json`);
first
.then(() => {
const h5pObject = require(`${dir}/h5p.json`);
const contentObject = require(`${dir}/content/content.json`);
return new H5P(libraryLoader).render(
name,
contentObject,
h5pObject
);
})
.then(h5p_page => res.end(h5p_page))
.catch(error => res.status(500).end(error.message));
});
let port = process.env.PORT || 8080;
server.listen(port, () => {
console.log('server running on http://localhost:' + port);
});