mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-07-29 19:12:27 +08:00
Allow setting CORS headers
This commit is contained in:
parent
0fa084711e
commit
07955daa1c
12
src/app.ts
12
src/app.ts
@ -7,6 +7,7 @@ import compression from "compression";
|
|||||||
import { fileURLToPath } from "url";
|
import { fileURLToPath } from "url";
|
||||||
import { dirname } from "path";
|
import { dirname } from "path";
|
||||||
import sessionParser from "./routes/session_parser.js";
|
import sessionParser from "./routes/session_parser.js";
|
||||||
|
import config from "./services/config.js";
|
||||||
import utils from "./services/utils.js";
|
import utils from "./services/utils.js";
|
||||||
import assets from "./routes/assets.js";
|
import assets from "./routes/assets.js";
|
||||||
import routes from "./routes/routes.js";
|
import routes from "./routes/routes.js";
|
||||||
@ -33,6 +34,17 @@ app.set("views", path.join(scriptDir, "views"));
|
|||||||
app.set("view engine", "ejs");
|
app.set("view engine", "ejs");
|
||||||
|
|
||||||
app.use((req, res, next) => {
|
app.use((req, res, next) => {
|
||||||
|
// set CORS header
|
||||||
|
if (config["Network"]["corsAllowOrigin"].length > 0) {
|
||||||
|
res.header("Access-Control-Allow-Origin", config["Network"]["corsAllowOrigin"]);
|
||||||
|
}
|
||||||
|
if (config["Network"]["corsAllowMethods"].length > 0) {
|
||||||
|
res.header("Access-Control-Allow-Methods", config["Network"]["corsAllowMethods"]);
|
||||||
|
}
|
||||||
|
if (config["Network"]["corsAllowHeaders"].length > 0) {
|
||||||
|
res.header("Access-Control-Allow-Headers", config["Network"]["corsAllowHeaders"]);
|
||||||
|
}
|
||||||
|
|
||||||
res.locals.t = t;
|
res.locals.t = t;
|
||||||
return next();
|
return next();
|
||||||
});
|
});
|
||||||
|
@ -29,6 +29,9 @@ export interface TriliumConfig {
|
|||||||
certPath: string;
|
certPath: string;
|
||||||
keyPath: string;
|
keyPath: string;
|
||||||
trustedReverseProxy: boolean | string;
|
trustedReverseProxy: boolean | string;
|
||||||
|
corsAllowOrigin: string;
|
||||||
|
corsAllowMethods: string;
|
||||||
|
corsAllowHeaders: string;
|
||||||
};
|
};
|
||||||
Session: {
|
Session: {
|
||||||
cookieMaxAge: number;
|
cookieMaxAge: number;
|
||||||
@ -79,7 +82,16 @@ const config: TriliumConfig = {
|
|||||||
process.env.TRILIUM_NETWORK_KEYPATH || iniConfig.Network.keyPath || "",
|
process.env.TRILIUM_NETWORK_KEYPATH || iniConfig.Network.keyPath || "",
|
||||||
|
|
||||||
trustedReverseProxy:
|
trustedReverseProxy:
|
||||||
process.env.TRILIUM_NETWORK_TRUSTEDREVERSEPROXY || iniConfig.Network.trustedReverseProxy || false
|
process.env.TRILIUM_NETWORK_TRUSTEDREVERSEPROXY || iniConfig.Network.trustedReverseProxy || false,
|
||||||
|
|
||||||
|
corsAllowOrigin:
|
||||||
|
process.env.TRILIUM_CORS_ALLOW_ORIGIN || iniConfig.Network.corsAllowOrigin || "",
|
||||||
|
|
||||||
|
corsAllowMethods:
|
||||||
|
process.env.TRILIUM_CORS_ALLOW_METHODS || iniConfig.Network.corsAllowMethods || "",
|
||||||
|
|
||||||
|
corsAllowHeaders:
|
||||||
|
process.env.TRILIUM_CORS_ALLOW_HEADERS || iniConfig.Network.corsAllowHeaders || ""
|
||||||
},
|
},
|
||||||
|
|
||||||
Session: {
|
Session: {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user