obolus/www/server.js

38 lines
1.0 KiB
JavaScript
Raw Normal View History

2019-05-22 18:34:58 +00:00
const { createServer } = require('http')
const httpProxy = require('http-proxy')
const { parse } = require('url')
const next = require('next')
const axios = require('axios')
// Initialize globals
axios.defaults.validateStatus = status => status < 500
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
const proxy = httpProxy.createProxyServer()
2019-05-23 11:19:42 +00:00
const target = dev
? `http://${process.env.API_HOST || 'api'}:5000`
: 'http://api'
2019-05-22 18:34:58 +00:00
app.prepare().then(() => {
createServer((req, res) => {
const parsedUrl = parse(req.url, true)
const { pathname } = parsedUrl
2019-05-27 18:18:26 +00:00
if (pathname === '/sw.js')
handle(req, res, parse('/static/sw.js', true))
else if (pathname.startsWith('/api/'))
2019-05-22 18:34:58 +00:00
proxy.web(req, res, { target }, error => console.log('Error!', error))
else
handle(req, res, parsedUrl)
}).listen(80, err => {
if (err) throw err
console.log('> Frontend Ready 🚀')
})
})