-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
88 lines (73 loc) · 2.41 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const chalk = require("chalk")
const express = require("express")
const next = require("next")
const i18next = require("./i18n/server")
const nextI18NextMiddleware = require("next-i18next/middleware").default
// const NextI18Next = require('next-i18next').default
// const nextI18NextOptions = require('./lib/nextI18NextOptions').nextI18NextOptions
// const nextI18next = new NextI18Next(nextI18NextOptions)
var os = require('os');
var ifaces = os.networkInterfaces();
var ip = '';
Object.keys(ifaces).forEach(function (ifname) {
var alias = 0;
ifaces[ifname].forEach(function (iface) {
if ('IPv4' !== iface.family || iface.internal !== false) {
// skip over internal (i.e. 127.0.0.1) and non-ipv4 addresses
return;
}
if (alias >= 1) {
// this single interface has multiple ipv4 addresses
// console.log(ifname + ':' + alias, iface.address);
ip = `${alias} ${iface.address}`
} else {
// this interface has only one ipv4 adress
// console.log(ifname, iface.address);
ip = iface.address
}
++alias;
});
});
const devProxy = {
"/.netlify": {
target: "http://localhost:9000",
pathRewrite: { "^/.netlify/functions": "" }
}
}
const port = parseInt(process.env.PORT, 10) || 3000
const env = process.env.NODE_ENV || 'development'
const dev = env !== "production"
const app = next({
dir: ".", // base directory where everything is, could move to src later
dev
})
const handle = app.getRequestHandler()
let server
app
.prepare()
.then(async () => {
server = express()
// Set up the proxy for Netlify lambda functions
if (dev && devProxy) {
// const proxyMiddleware = require("http-proxy-middleware")
const { createProxyMiddleware } = require('http-proxy-middleware')
Object.keys(devProxy).forEach(function (context) {
server.use(createProxyMiddleware(context, devProxy[context]))
})
}
await i18next.initPromise
server.use(nextI18NextMiddleware(i18next))
// server.use(nextI18NextMiddleware(nextI18next))
// Default catch-all handler to allow Next.js to handle all other routes
server.all("*", (req, res) => handle(req, res))
server.listen(port, err => {
if (err) {
throw err
}
console.log(chalk.yellow(`> Ready on http://${ip}:${port} [${env}]`))
})
})
.catch(err => {
console.log("An error occurred, unable to start the server")
console.log(err)
})