-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.mjs
69 lines (56 loc) · 1.84 KB
/
index.mjs
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
import bodyParser from 'body-parser';
import express from 'express';
import morgan from 'morgan';
import path from 'path';
import url from 'url';
import createRouter from './lib/create-router.mjs';
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
const port = process.env.PORT || 3000;
const repositoryPath = process.env.REPOSITORY_PATH || './repository';
const adminPassword = process.env.ADMIN_PASSWORD || '';
const pathPrefix = process.env.ROOT_PATH || '/';
const bodySizeLimit = process.env.BODY_SIZE_LIMIT || '10mb';
const timeoutMillisec = Number(process.env.SERVER_TIMEOUT || 0);
const router = createRouter(
repositoryPath,
adminPassword,
pathPrefix,
timeoutMillisec
);
const app = express();
app.set('query parser', 'extended');
app.set('json spaces', 2);
if (process.env.NODE_ENV === 'production') {
app.use(morgan('combined'));
} else {
app.use(morgan('dev'));
}
app.use(
bodyParser.json({ type: 'application/vnd.api+json', limit: bodySizeLimit })
);
app.use(bodyParser.urlencoded({ extended: true, limit: bodySizeLimit }));
app.use(pathPrefix, router);
app.use(pathPrefix, express.static(__dirname + '/public'));
app.get('/*', async (req, res) => {
res.sendFile('index.html', { root: __dirname + '/public' });
});
app.use((err, req, res, next) => {
if (req.timedout && !res.headerSent) {
res.status(503).json({ error: 'request timeout' });
} else {
if (err.code === 'ENOENT') {
res.sendStatus(404);
} else {
console.log('ERROR', err);
res.status(422).json({ errors: [{ status: 500, detail: err.message }] });
}
}
});
app.listen(port, () => {
const timeoutMessage = timeoutMillisec
? `timeout in ${timeoutMillisec}ms`
: `no timeout`;
console.log(
`listening on ${port}, repository is at ${repositoryPath}, body size limit ${bodySizeLimit}, ${timeoutMessage}`
);
});