Need a nasty little handler to catch people (reasonably so) typing in localhost instead of 127.0.0.1. This is needed because socket.io defaults to IPVv6 on Node 18 for some reason, and probably others.

main
Zed A. Shaw 2 years ago
parent 375d7a2d80
commit 17e05130e3
  1. 16
      commands/api.js

@ -13,6 +13,7 @@ import devtools from '../lib/devtools.js';
import * as acorn from "acorn";
import fs from "fs";
import cors from "cors";
import { base_host } from "../client/config.js";
import { createHttpTerminator } from 'http-terminator';
const app = express();
@ -96,6 +97,21 @@ export const main = async (opts) => {
if(opts.DANGER_ADMIN) {
const { media_servers } = await import("../lib/config.js");
app.use(cors({origin: media_servers}));
// this is necessary to deal with Node 18 defaulting to IPv6 for "localhost"
app.use((req, res, next) => {
const full_host = `${req.protocol}://${req.get('host')}`;
if(full_host !== base_host) {
const redirect_to = new URL(req.url, base_host);
log.error(`Your client/config.js:base_host is ${base_host} but you are accessing the site at ${full_host}. Redirecting to ${redirect_to} so cookies will function!`);
res.redirect(redirect_to);
} else {
next();
}
})
}
if(opts.debugHeaders) {

Loading…
Cancel
Save