-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.ts
43 lines (39 loc) · 1.26 KB
/
server.ts
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
import next from 'next';
import bodyParser from 'body-parser';
import express from 'express';
import graphqlProxyMiddleware from './server/middlewares/graphql';
import { DEV, GRAPHQL_APP_URL, PORT } from './config/vars';
// Create body-parser json middleware
const bodyParserJSON = bodyParser.json();
// Create the Express-Next App
const app = next({ dev: DEV });
const handle = app.getRequestHandler();
// Start the app
app
.prepare()
// Start Express server and serve the
.then(() => {
express()
// use proxy middleware to send graphql requests to api server
.use(GRAPHQL_APP_URL, bodyParserJSON, graphqlProxyMiddleware)
.use((req, res, cb) => {
const schema = req.headers['x-forwarded-proto'];
if (req.headers.host && req.headers.host.indexOf('localhost') < 0 && schema !== 'https') {
res.redirect(`https://${req.headers.host}${req.url}`);
} else {
cb();
}
})
.use((req, res) => {
return handle(req, res);
})
.listen(PORT, (error?: Error) => {
if (error) throw error;
// eslint-disable-next-line no-console
console.log(`> Ready on http://localhost:${PORT}`);
});
})
.catch(ex => {
console.error(ex.stack);
process.exit(1);
});