forked from ApryseSDK/webviewer-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dev-server.js
87 lines (75 loc) · 2.51 KB
/
dev-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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const fs = require('fs');
const path = require('path');
const express = require('express');
const bodyParser = require('body-parser');
const webpack = require('webpack');
const config = require('./webpack.config.dev');
const devMiddleware = require('webpack-dev-middleware');
const hotMiddleware = require('webpack-hot-middleware');
const ip = require('ip');
const opn = require('opn');
const app = express();
const compiler = webpack(config);
app.use(devMiddleware(compiler, {
logLevel: 'warn',
publicPath: config.output.publicPath,
watchOptions: {
aggregateTimeout: 300,
poll: true
}
}));
app.use(hotMiddleware(compiler));
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.use('/i18n', express.static(path.resolve(__dirname, 'i18n')));
app.use('/assets', express.static(path.resolve(__dirname, 'assets')));
app.use('/mime', express.static(path.resolve(__dirname, 'mime')));
app.use('/core', express.static(path.resolve(__dirname, '../core')));
app.use('/files', express.static(path.resolve(__dirname, '../../samples/files')));
const handleAnnotation = (req, res, handler) => {
const dir = path.resolve(__dirname, 'annotations');
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
handler(dir);
res.end();
};
app.get('/annotations', (req, res) => {
handleAnnotation(req, res, dir => {
const xfdfFile = (req.query.did) ? path.resolve(dir, req.query.did + '.xfdf') : path.resolve(dir, 'default.xfdf');
if (fs.existsSync(xfdfFile)) {
res.header('Content-Type', 'text/xml');
res.send(fs.readFileSync(xfdfFile));
} else {
res.status(204);
}
});
});
app.post('/annotations', (req, res) => {
handleAnnotation(req, res, dir => {
const xfdfFile = (req.body.did) ? path.resolve(dir, req.body.did + '.xfdf') : path.resolve(dir, 'default.xfdf');
try {
res.send(fs.writeFileSync(xfdfFile, req.body.data));
} catch (e) {
res.status(500);
}
});
res.end();
});
app.get('/license-key.js', (req, res) => {
res.sendFile(path.resolve(__dirname, '../../samples/license-key.js'));
});
app.get('/', (req, res) => {
res.sendFile(path.resolve(__dirname, 'src/index.html'));
});
app.get('/mobile', (req, res) => {
res.redirect(`/#d=/files/webviewer-demo-annotated.xod&a=1`);
});
app.listen(3000, '0.0.0.0', err => {
if (err) {
console.error(err);
} else {
console.info(`Listening at localhost:3000 (http://${ip.address()}:3000)`);
opn('http://localhost:3000/#d=/files/webviewer-demo-annotated.xod&a=1');
}
});