forked from CEN3031-FINAL/Emerald-Project15-10d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
52 lines (47 loc) · 1.49 KB
/
index.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
'use strict';
/**
* Module dependencies
*/
const fs = require('fs');
const path = require('path');
module.exports = (strapi) => {
return {
/**
* Initialize the hook
*/
initialize() {
strapi.app.use(async (ctx, next) => {
const reqPath = ctx.request.path;
const reqHost = ctx.request.header.host;
const reqReferer = ctx.request.header.referer;
const refererUrl = reqReferer
? reqReferer
.replace('http://', '')
.replace('https://', '')
.replace(reqHost, '')
: '';
if (
reqPath === '/favicon.ico' ||
reqPath.startsWith('/admin') ||
reqPath.startsWith('/client') ||
refererUrl.startsWith('/admin') ||
reqPath.startsWith('/documentation') ||
refererUrl.startsWith('/documentation')
) {
// if request for favicon, admin, or client or if request from admin, go next
await next();
} else if (reqPath.startsWith('/api')) {
// if api request, remove /api
ctx.request.path = reqPath.replace('/api', '');
await next();
} else {
// serve the index.html for the client route
const { clientPath } = strapi.config.middleware.settings.proxy;
const clientDir = path.resolve(strapi.dir, clientPath);
ctx.type = 'html';
ctx.body = fs.createReadStream(path.join(clientDir + '/index.html'));
}
});
},
};
};