forked from jsdevkr/gitCodeShare.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfront.ts
More file actions
61 lines (50 loc) · 1.59 KB
/
front.ts
File metadata and controls
61 lines (50 loc) · 1.59 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
58
59
60
61
import 'isomorphic-unfetch';
import express from 'express';
import morgan from 'morgan';
import path from 'path';
import next from 'next';
import proxy from 'http-proxy-middleware';
import { useStaticRendering } from 'mobx-react';
useStaticRendering(true);
const port = parseInt(process.env.FRONT_PORT, 10) || 3000;
const dev = process.env.NODE_ENV !== 'production';
const proxyContext = process.env.BACKEND_PROXY_CONTEXT || '/api';
const app = next({ dev });
const handle = app.getRequestHandler();
process.on('SIGINT', () => process.exit());
if (!dev) {
const LOGS_ID = `${process.env.LOGS_SECRET_PREFIX}:${process.env.NOW_URL}`;
require('now-logs')(LOGS_ID);
}
app.prepare().then(() => {
const server = express();
if (dev) {
server.use(morgan('tiny'));
}
// proxy
server.use(
`${proxyContext}`,
proxy({
target: `http://localhost:${process.env.BACKEND_PORT || 3030}`,
onError: (err, req, res) => {
if (!res.headersSent) {
if (typeof res.writeHead === 'function') {
res.writeHead(500, { 'content-type': 'application/json' });
}
}
const json = { error: 'proxy_error', reason: err.message };
res.end(JSON.stringify(json));
},
}),
);
const filePath = path.join(__dirname, '.next', 'service-worker.js');
server.get('/service-worker.js', (req, res) => app.serveStatic(req, res, filePath));
// to next.js
server.get('*', (req, res) => handle(req, res));
server.listen(port, '0.0.0.0', err => {
if (err) {
throw err;
}
console.log(`> Ready on http://localhost:${port}`);
});
});