fix: cors settings for POST

This commit is contained in:
mattia
2025-01-05 13:30:36 +01:00
parent 93ed3a695e
commit 5ff6a38554
3 changed files with 13 additions and 8 deletions

View File

@@ -1,5 +1,9 @@
HTTP_PORT=9283
# Host and port this server will listen on
HTTP_HOST=localhost
HTTP_PORT=9283
# Allowed origins for POST requests; needed for web games; if empty or missing, defaults to '*'
HTTP_POST_ALLOWED_ORIGINS=https://your.domain.com
# Database connection settings
DATABASE_NAME=game_logger
DATABASE_HOST=localhost
DATABASE_USER=game_logger

View File

@@ -11,11 +11,6 @@ const app = express();
// serve che client files
app.use(express.static("./dist/client"));
// activate cors, but only if in dev mode (in prodution, the client is served by the same server)
if (process.env.TS_NODE_DEV) {
app.use(cors());
}
// enable body parser to parse json and urlencoded data
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

View File

@@ -1,12 +1,18 @@
import type { Express } from "express";
import multer from "multer";
import cors from "cors";
import cors, { type CorsOptions } from "cors";
import { addEntry } from "./db";
const upload = multer();
const postCorsOptions: CorsOptions = {
origin: (process.env.HTTP_POST_ALLOWED_ORIGINS || "*").split(","),
methods: ["POST"],
};
export function registerPostMessage(app: Express) {
app.post("/", cors(), (req, res) => {
app.options("/", cors(postCorsOptions));
app.post("/", cors(postCorsOptions), (req, res) => {
console.log("\nReceived logging message:");
const metadata: [string, string][] = [];
let gameName: string = "";