-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
52 lines (44 loc) · 1.33 KB
/
server.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
const express = require('express');
const next = require('next');
const port = parseInt(process.env.PORT, 10) || 3000;
const dev = process.env.BABEL_ENV !== 'prod';
const app = next({ dev });
const handle = app.getRequestHandler();
const devProxy = {
'/api': {
// target: 'http://localhost:7100/',
target: 'https://api-dev.dmka.cn/',
secure: false,
pathRewrite: { '^/api': '/' },
changeOrigin: true,
},
};
app.prepare().then(() => {
const server = express();
// Set up the proxy.
if (dev && devProxy) {
const proxyMiddleware = require('http-proxy-middleware');
Object.keys(devProxy).forEach(context => {
server.use(proxyMiddleware(context, devProxy[context]));
});
}
// Default catch-all handler to allow Next.js to handle all other routes
// server.all('*', (req, res) => handle(req, res));
//
server.get('/a', (req, res) => {
return app.render(req, res, '/b', req.query);
});
server.get('/b', (req, res) => {
return app.render(req, res, '/test', req.query);
});
// server.get('/posts/:id', (req, res) => {
// return app.render(req, res, '/posts', { id: req.params.id });
// });
server.get('*', (req, res) => {
return handle(req, res);
});
server.listen(port, err => {
if (err) throw err;
console.log(`> Ready on http://localhost:${port}`);
});
});