Skip to content
This repository was archived by the owner on Jun 24, 2025. It is now read-only.

Commit 07955da

Browse files
committed
Allow setting CORS headers
1 parent 0fa0847 commit 07955da

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/app.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import compression from "compression";
77
import { fileURLToPath } from "url";
88
import { dirname } from "path";
99
import sessionParser from "./routes/session_parser.js";
10+
import config from "./services/config.js";
1011
import utils from "./services/utils.js";
1112
import assets from "./routes/assets.js";
1213
import routes from "./routes/routes.js";
@@ -33,6 +34,17 @@ app.set("views", path.join(scriptDir, "views"));
3334
app.set("view engine", "ejs");
3435

3536
app.use((req, res, next) => {
37+
// set CORS header
38+
if (config["Network"]["corsAllowOrigin"].length > 0) {
39+
res.header("Access-Control-Allow-Origin", config["Network"]["corsAllowOrigin"]);
40+
}
41+
if (config["Network"]["corsAllowMethods"].length > 0) {
42+
res.header("Access-Control-Allow-Methods", config["Network"]["corsAllowMethods"]);
43+
}
44+
if (config["Network"]["corsAllowHeaders"].length > 0) {
45+
res.header("Access-Control-Allow-Headers", config["Network"]["corsAllowHeaders"]);
46+
}
47+
3648
res.locals.t = t;
3749
return next();
3850
});

src/services/config.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ export interface TriliumConfig {
2929
certPath: string;
3030
keyPath: string;
3131
trustedReverseProxy: boolean | string;
32+
corsAllowOrigin: string;
33+
corsAllowMethods: string;
34+
corsAllowHeaders: string;
3235
};
3336
Session: {
3437
cookieMaxAge: number;
@@ -79,7 +82,16 @@ const config: TriliumConfig = {
7982
process.env.TRILIUM_NETWORK_KEYPATH || iniConfig.Network.keyPath || "",
8083

8184
trustedReverseProxy:
82-
process.env.TRILIUM_NETWORK_TRUSTEDREVERSEPROXY || iniConfig.Network.trustedReverseProxy || false
85+
process.env.TRILIUM_NETWORK_TRUSTEDREVERSEPROXY || iniConfig.Network.trustedReverseProxy || false,
86+
87+
corsAllowOrigin:
88+
process.env.TRILIUM_CORS_ALLOW_ORIGIN || iniConfig.Network.corsAllowOrigin || "",
89+
90+
corsAllowMethods:
91+
process.env.TRILIUM_CORS_ALLOW_METHODS || iniConfig.Network.corsAllowMethods || "",
92+
93+
corsAllowHeaders:
94+
process.env.TRILIUM_CORS_ALLOW_HEADERS || iniConfig.Network.corsAllowHeaders || ""
8395
},
8496

8597
Session: {

0 commit comments

Comments
 (0)