-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
36 lines (30 loc) · 993 Bytes
/
app.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
/* eslint-disable import/no-dynamic-require, global-require */
import express from 'express';
import path from 'path';
import fs from 'fs';
const app = express();
// middleware to load the required endpoint based on the request URL
const loadApp = (req, res, next) => {
const match = req.url.match(/\/api\/(v[0-9]+).*/) || [];
const version = match[1] || '';
if (version !== '') {
const appPath = process.env.NODE_ENV !== 'production'
? path.join(__dirname, `./server/api/${version}/index.js`)
: path.join(__dirname, `./api/${version}/index.js`);
const pathExists = fs.existsSync(appPath);
if (!pathExists) {
return res.status(404).send({
message: 'The requested endpoint does not exist',
});
}
require(appPath).default(app, express);
} else {
// require('./client/index').default(app)
return res.status(200).send({
message: 'Loaded the client app',
});
}
next();
};
app.use(loadApp);
export default app;