From e5400296cdcfe791b78ab8a815b5e7849560d381 Mon Sep 17 00:00:00 2001
From: Marcus Schiesser <mail@marcusschiesser.de>
Date: Mon, 13 Nov 2023 11:13:15 +0700
Subject: [PATCH] fix: add cors to express app

---
 .../types/simple/express/README-template.md      | 16 ++++++++++++++++
 templates/types/simple/express/index.ts          |  8 ++++++++
 templates/types/simple/express/package.json      |  2 ++
 .../types/streaming/express/README-template.md   | 16 ++++++++++++++++
 templates/types/streaming/express/index.ts       |  8 ++++++++
 templates/types/streaming/express/package.json   |  2 ++
 6 files changed, 52 insertions(+)

diff --git a/templates/types/simple/express/README-template.md b/templates/types/simple/express/README-template.md
index 98ebc4e4..7ea94ab7 100644
--- a/templates/types/simple/express/README-template.md
+++ b/templates/types/simple/express/README-template.md
@@ -24,6 +24,22 @@ curl --location 'localhost:8000/api/chat' \
 
 You can start editing the API by modifying `src/controllers/chat.controller.ts`. The endpoint auto-updates as you save the file.
 
+## Production
+
+First, build the project:
+
+```
+npm run build
+```
+
+You can then run the production server:
+
+```
+NODE_ENV=production npm run start
+```
+
+> Note that the `NODE_ENV` environment variable is set to `production`. This disables CORS for all origins.
+
 ## Learn More
 
 To learn more about LlamaIndex, take a look at the following resources:
diff --git a/templates/types/simple/express/index.ts b/templates/types/simple/express/index.ts
index a1bef65d..70a43ab5 100644
--- a/templates/types/simple/express/index.ts
+++ b/templates/types/simple/express/index.ts
@@ -1,3 +1,4 @@
+import cors from "cors";
 import "dotenv/config";
 import express, { Express, Request, Response } from "express";
 import chatRouter from "./src/routes/chat.route";
@@ -5,6 +6,13 @@ import chatRouter from "./src/routes/chat.route";
 const app: Express = express();
 const port = 8000;
 
+const env = process.env["NODE_ENV"];
+const isDevelopment = !env || env === "development";
+if (isDevelopment) {
+  console.warn("Running in development mode - allowing CORS for all origins");
+  app.use(cors());
+}
+
 app.use(express.json());
 
 app.get("/", (req: Request, res: Response) => {
diff --git a/templates/types/simple/express/package.json b/templates/types/simple/express/package.json
index 05b6eda8..55902071 100644
--- a/templates/types/simple/express/package.json
+++ b/templates/types/simple/express/package.json
@@ -9,11 +9,13 @@
     "dev": "concurrently \"tsup index.ts --format esm --dts --watch\" \"nodemon -q dist/index.js\""
   },
   "dependencies": {
+    "cors": "^2.8.5",
     "dotenv": "^16.3.1",
     "express": "^4",
     "llamaindex": "0.0.31"
   },
   "devDependencies": {
+    "@types/cors": "^2.8.16",
     "@types/express": "^4",
     "@types/node": "^20",
     "concurrently": "^8",
diff --git a/templates/types/streaming/express/README-template.md b/templates/types/streaming/express/README-template.md
index 98ebc4e4..7ea94ab7 100644
--- a/templates/types/streaming/express/README-template.md
+++ b/templates/types/streaming/express/README-template.md
@@ -24,6 +24,22 @@ curl --location 'localhost:8000/api/chat' \
 
 You can start editing the API by modifying `src/controllers/chat.controller.ts`. The endpoint auto-updates as you save the file.
 
+## Production
+
+First, build the project:
+
+```
+npm run build
+```
+
+You can then run the production server:
+
+```
+NODE_ENV=production npm run start
+```
+
+> Note that the `NODE_ENV` environment variable is set to `production`. This disables CORS for all origins.
+
 ## Learn More
 
 To learn more about LlamaIndex, take a look at the following resources:
diff --git a/templates/types/streaming/express/index.ts b/templates/types/streaming/express/index.ts
index a1bef65d..70a43ab5 100644
--- a/templates/types/streaming/express/index.ts
+++ b/templates/types/streaming/express/index.ts
@@ -1,3 +1,4 @@
+import cors from "cors";
 import "dotenv/config";
 import express, { Express, Request, Response } from "express";
 import chatRouter from "./src/routes/chat.route";
@@ -5,6 +6,13 @@ import chatRouter from "./src/routes/chat.route";
 const app: Express = express();
 const port = 8000;
 
+const env = process.env["NODE_ENV"];
+const isDevelopment = !env || env === "development";
+if (isDevelopment) {
+  console.warn("Running in development mode - allowing CORS for all origins");
+  app.use(cors());
+}
+
 app.use(express.json());
 
 app.get("/", (req: Request, res: Response) => {
diff --git a/templates/types/streaming/express/package.json b/templates/types/streaming/express/package.json
index b473b0eb..72f127b4 100644
--- a/templates/types/streaming/express/package.json
+++ b/templates/types/streaming/express/package.json
@@ -10,11 +10,13 @@
   },
   "dependencies": {
     "ai": "^2",
+    "cors": "^2.8.5",
     "dotenv": "^16.3.1",
     "express": "^4",
     "llamaindex": "0.0.31"
   },
   "devDependencies": {
+    "@types/cors": "^2.8.16",
     "@types/express": "^4",
     "@types/node": "^20",
     "concurrently": "^8",
-- 
GitLab