Files
MHDMap/server/index.js

68 lines
1.8 KiB
JavaScript

import ParcelCore from "@parcel/core";
const { default: Parcel } = ParcelCore;
import { createProxyMiddleware } from 'http-proxy-middleware';
import express from "express";
import { createRequire } from "module";
import path from 'path';
import { fileURLToPath } from 'url';
import config from "../config.js";
import MhdMapApp from "./MhdMapApp.js";
import SocketHandler from "./SocketHandler.js";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const devPort = config.devPort;
const servicePort = config.servicePort;
const staticFilePath = './static'
// Development Server Settings
const frontEndDevServerOptions = {
defaultConfig: createRequire(import.meta.url).resolve(
"@parcel/config-default"
),
entries: path.join(__dirname, '../client/index.html'),
mode: 'development',
logLevel: 4,
serveOptions: {
host: '127.0.0.1',
port: devPort,
},
/* hmrOptions: {
port: devPort,
}, */
publicUrl: config.urlPrefix,
defaultTargetOptions: {
publicUrl: config.urlPrefix,
},
};
const bundler = new Parcel(frontEndDevServerOptions);
(async () => {
await bundler.watch();
})();
const server = express();
const socketHandler = new SocketHandler(config.urlPrefix + '/ws');
socketHandler.start(server);
const app = new MhdMapApp(socketHandler);
app.start();
server.use(config.urlPrefix + '/static', express.static(staticFilePath));
const proxyCfg = {
target: `http://127.0.0.1:${devPort}/`,
pathRewrite: {},
};
proxyCfg.pathRewrite['^' + config.urlPrefix] = '/'; // TODO: Maybe fix this with Parcel
const parcelMiddleware = createProxyMiddleware(proxyCfg);
server.use('/', parcelMiddleware);
// Run your Express server
server.listen(servicePort, '127.0.0.1', () => {
console.log(`Listening to port ${servicePort}...`);
});