This repository was archived by the owner on Jun 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 372
Expand file tree
/
Copy pathexpress.js
More file actions
57 lines (48 loc) · 1.68 KB
/
express.js
File metadata and controls
57 lines (48 loc) · 1.68 KB
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
import express from 'express';
import path from 'path';
import compression from 'compression';
import bodyParser from 'body-parser';
import logger from 'morgan';
import favicon from 'serve-favicon';
import useragent from 'express-useragent';
import cookieParser from 'cookie-parser';
import cors from 'cors';
import httpProxy from 'http-proxy';
import sitemap from './sitemap';
import support from './support';
const proxyApi = httpProxy.createProxyServer({
target: process.env.API_URL,
secure: true
});
proxyApi.on('error', (error, req, res) => {
let json;
if (error.code !== 'ECONNRESET') {
console.error('proxy error', error);
}
if (!res.headersSent) {
res.writeHead(500, {'content-type': 'application/json'});
}
json = {error: 'proxy_error', reason: error.message};
res.end(JSON.stringify(json));
});
export default function(server) {
server.use(compression());
server.use(bodyParser.json());
server.use(logger('dev'));
server.use(useragent.express());
server.use(cookieParser());
server.use(cors());
// Static content
server.use(favicon(path.join((process.env.PWD || process.env.pm_cwd) , '/static/images/favicon.ico')));
server.use(express.static(path.join(process.env.PWD || process.env.pm_cwd, '/static')));
server.use('/public', express.static(path.join((process.env.PWD || process.env.pm_cwd), '/static/dist')));
// server.use('/build', express.static(path.join((process.env.PWD || process.env.pm_cwd), '/static/dist')));
sitemap(server);
support(server);
server.get(/^\/(images|fonts)\/.*/, function(req, res) {
res.redirect(301, '//quran-1f14.kxcdn.com' + req.path);
});
server.use('/api', (req, res) => {
proxyApi.web(req, res);
});
}