-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpress.js
36 lines (27 loc) · 1.04 KB
/
express.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
'use strict';
const Backend = require('./backend');
const express = require('express');
const frontend = require('./frontend');
const { toRoute } = require('extrovert');
module.exports = function(apiUrl, conn, options) {
const router = express.Router();
apiUrl = apiUrl || '/admin/api';
const backend = Backend(conn);
const apiRouter = express.Router();
apiRouter.use(express.json());
for (const [name, actions] of Object.entries(backend)) {
const subrouter = express.Router();
for (const [actionName, action] of Object.entries(actions)) {
subrouter.options(`/${actionName}`, (req, res) => res.send(''));
subrouter.get(`/${actionName}`, toRoute(action));
subrouter.put(`/${actionName}`, toRoute(action));
subrouter.post(`/${actionName}`, toRoute(action));
subrouter.delete(`/${actionName}`, toRoute(action));
}
apiRouter.use(`/${name}`, subrouter);
}
router.use('/api', apiRouter);
frontend(apiUrl, false, options);
router.use(express.static(`${__dirname}/frontend/public`));
return router;
}