fix: cors settings for POST
This commit is contained in:
@@ -1,5 +1,9 @@
|
|||||||
HTTP_PORT=9283
|
# Host and port this server will listen on
|
||||||
HTTP_HOST=localhost
|
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_NAME=game_logger
|
||||||
DATABASE_HOST=localhost
|
DATABASE_HOST=localhost
|
||||||
DATABASE_USER=game_logger
|
DATABASE_USER=game_logger
|
||||||
|
|||||||
@@ -11,11 +11,6 @@ const app = express();
|
|||||||
// serve che client files
|
// serve che client files
|
||||||
app.use(express.static("./dist/client"));
|
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
|
// enable body parser to parse json and urlencoded data
|
||||||
app.use(bodyParser.urlencoded({ extended: false }));
|
app.use(bodyParser.urlencoded({ extended: false }));
|
||||||
app.use(bodyParser.json());
|
app.use(bodyParser.json());
|
||||||
|
|||||||
@@ -1,12 +1,18 @@
|
|||||||
import type { Express } from "express";
|
import type { Express } from "express";
|
||||||
import multer from "multer";
|
import multer from "multer";
|
||||||
import cors from "cors";
|
import cors, { type CorsOptions } from "cors";
|
||||||
import { addEntry } from "./db";
|
import { addEntry } from "./db";
|
||||||
|
|
||||||
const upload = multer();
|
const upload = multer();
|
||||||
|
|
||||||
|
const postCorsOptions: CorsOptions = {
|
||||||
|
origin: (process.env.HTTP_POST_ALLOWED_ORIGINS || "*").split(","),
|
||||||
|
methods: ["POST"],
|
||||||
|
};
|
||||||
|
|
||||||
export function registerPostMessage(app: Express) {
|
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:");
|
console.log("\nReceived logging message:");
|
||||||
const metadata: [string, string][] = [];
|
const metadata: [string, string][] = [];
|
||||||
let gameName: string = "";
|
let gameName: string = "";
|
||||||
|
|||||||
Reference in New Issue
Block a user