Skip to content
Snippets Groups Projects
Commit e5400296 authored by Marcus Schiesser's avatar Marcus Schiesser
Browse files

fix: add cors to express app

parent 200e99db
No related branches found
No related tags found
No related merge requests found
...@@ -24,6 +24,22 @@ curl --location 'localhost:8000/api/chat' \ ...@@ -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. 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 ## Learn More
To learn more about LlamaIndex, take a look at the following resources: To learn more about LlamaIndex, take a look at the following resources:
......
import cors from "cors";
import "dotenv/config"; import "dotenv/config";
import express, { Express, Request, Response } from "express"; import express, { Express, Request, Response } from "express";
import chatRouter from "./src/routes/chat.route"; import chatRouter from "./src/routes/chat.route";
...@@ -5,6 +6,13 @@ import chatRouter from "./src/routes/chat.route"; ...@@ -5,6 +6,13 @@ import chatRouter from "./src/routes/chat.route";
const app: Express = express(); const app: Express = express();
const port = 8000; 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.use(express.json());
app.get("/", (req: Request, res: Response) => { app.get("/", (req: Request, res: Response) => {
......
...@@ -9,11 +9,13 @@ ...@@ -9,11 +9,13 @@
"dev": "concurrently \"tsup index.ts --format esm --dts --watch\" \"nodemon -q dist/index.js\"" "dev": "concurrently \"tsup index.ts --format esm --dts --watch\" \"nodemon -q dist/index.js\""
}, },
"dependencies": { "dependencies": {
"cors": "^2.8.5",
"dotenv": "^16.3.1", "dotenv": "^16.3.1",
"express": "^4", "express": "^4",
"llamaindex": "0.0.31" "llamaindex": "0.0.31"
}, },
"devDependencies": { "devDependencies": {
"@types/cors": "^2.8.16",
"@types/express": "^4", "@types/express": "^4",
"@types/node": "^20", "@types/node": "^20",
"concurrently": "^8", "concurrently": "^8",
......
...@@ -24,6 +24,22 @@ curl --location 'localhost:8000/api/chat' \ ...@@ -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. 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 ## Learn More
To learn more about LlamaIndex, take a look at the following resources: To learn more about LlamaIndex, take a look at the following resources:
......
import cors from "cors";
import "dotenv/config"; import "dotenv/config";
import express, { Express, Request, Response } from "express"; import express, { Express, Request, Response } from "express";
import chatRouter from "./src/routes/chat.route"; import chatRouter from "./src/routes/chat.route";
...@@ -5,6 +6,13 @@ import chatRouter from "./src/routes/chat.route"; ...@@ -5,6 +6,13 @@ import chatRouter from "./src/routes/chat.route";
const app: Express = express(); const app: Express = express();
const port = 8000; 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.use(express.json());
app.get("/", (req: Request, res: Response) => { app.get("/", (req: Request, res: Response) => {
......
...@@ -10,11 +10,13 @@ ...@@ -10,11 +10,13 @@
}, },
"dependencies": { "dependencies": {
"ai": "^2", "ai": "^2",
"cors": "^2.8.5",
"dotenv": "^16.3.1", "dotenv": "^16.3.1",
"express": "^4", "express": "^4",
"llamaindex": "0.0.31" "llamaindex": "0.0.31"
}, },
"devDependencies": { "devDependencies": {
"@types/cors": "^2.8.16",
"@types/express": "^4", "@types/express": "^4",
"@types/node": "^20", "@types/node": "^20",
"concurrently": "^8", "concurrently": "^8",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment