Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathfront.ts
More file actions
Latest commit
54 lines (45 loc) · 1.42 KB
/
Copy pathfront.ts
File metadata and controls
54 lines (45 loc) · 1.42 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
importexpressfrom'express';
importmorganfrom'morgan';
importpathfrom'path';
importnextfrom'next';
importproxyfrom'http-proxy-middleware';
import{useStaticRendering}from'mobx-react';
useStaticRendering(true);
process.on('SIGINT',()=>process.exit());
constport=parseInt(process.env.FRONT_PORT,10)||3000;
constdev=process.env.NODE_ENV!=='production';
constproxyContext=process.env.BACKEND_PROXY_CONTEXT||'/api';
constapp=next({ dev });
consthandle=app.getRequestHandler();
app.prepare().then(()=>{
constserver=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(typeofres.writeHead==='function'){
res.writeHead(500,{'content-type': 'application/json'});
}
}
constjson={error: 'proxy_error',reason: err.message};
res.end(JSON.stringify(json));
},
}),
);
constfilePath=path.join(__dirname,'.next','service-worker.js');
server.get('/service-worker.js',(req,res)=>app.serveStatic(req,res,filePath));
// to next.js
server.get('*',handleasany);
server.listen(port,'0.0.0.0',err=>{
if(err){
throwerr;
}
console.log(`> Ready on http://localhost:${port}`);
});
});