1
0
mirror of https://github.com/checktheroads/hyperglass synced 2024-05-11 05:55:08 +00:00
Files
checktheroads-hyperglass/hyperglass/ui/nextdev.js

53 lines
1.5 KiB
JavaScript
Raw Normal View History

2020-01-19 22:00:33 -07:00
/* eslint-disable no-console */
2020-10-07 09:41:58 -07:00
const express = require('express');
const proxyMiddleware = require('http-proxy-middleware');
const next = require('next');
const envVars = require('/tmp/hyperglass.env.json');
const { configFile } = envVars;
const config = require(String(configFile));
const { NODE_ENV: env, _HYPERGLASS_URL_: envUrl } = config;
2020-01-19 22:00:33 -07:00
const devProxy = {
2020-10-07 09:41:58 -07:00
'/api/query/': { target: envUrl + 'api/query/', pathRewrite: { '^/api/query/': '' } },
'/images': { target: envUrl + 'images', pathRewrite: { '^/images': '' } },
'/custom': { target: envUrl + 'custom', pathRewrite: { '^/custom': '' } },
2020-01-19 22:00:33 -07:00
};
const port = parseInt(process.env.PORT, 10) || 3000;
2020-10-07 09:41:58 -07:00
const dev = env !== 'production';
2020-01-19 22:00:33 -07:00
const app = next({
2020-10-07 09:41:58 -07:00
dir: '.', // base directory where everything is, could move to src later
dev,
2020-01-19 22:00:33 -07:00
});
const handle = app.getRequestHandler();
let server;
2020-10-07 09:41:58 -07:00
app
.prepare()
.then(() => {
server = express();
// Set up the proxy.
if (dev && devProxy) {
Object.keys(devProxy).forEach(function(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.listen(port, err => {
if (err) {
throw err;
}
console.log(`> Ready on port ${port} [${env}]`);
2020-01-19 22:00:33 -07:00
});
2020-10-07 09:41:58 -07:00
})
.catch(err => {
console.log('An error occurred, unable to start the server');
console.log(err);
});