diff --git a/templates/types/simple/express/README-template.md b/templates/types/simple/express/README-template.md index 98ebc4e49c9545ab03c2ec08d1c01028363b11a7..7ea94ab755535aefd4d5edc9f20a5908d7fedead 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 a1bef65d6526065382b75171952d6428fd15bce2..70a43ab580aff93df60948076464b9c35bb6f167 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 05b6eda8be8cf9f8964dbe54cfb279eae39e9018..55902071463959a02df82ad72ae516d0e385bc78 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 98ebc4e49c9545ab03c2ec08d1c01028363b11a7..7ea94ab755535aefd4d5edc9f20a5908d7fedead 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 a1bef65d6526065382b75171952d6428fd15bce2..70a43ab580aff93df60948076464b9c35bb6f167 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 b473b0eb217c1a2be5131b10317c3c26bd1deba6..72f127b407a8c7ce675bf4d634a42f57af78bb4c 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",