Skip to content

Commit 3d9cb8f

Browse files
committed
feature add index.js in server
1 parent 2a2eb64 commit 3d9cb8f

File tree

4 files changed

+870
-1
lines changed

4 files changed

+870
-1
lines changed

server/api.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import {Configuration , OpenAIApi} from 'openai';
2+
import dotenv from 'dotenv';
3+
4+
dotenv.config();
5+
6+
const openaiApiKey = process.env.OPENAI_API_KEY;
7+
8+
if (!openaiApiKey) {
9+
console.error('OPENAI_API_KEY is not defined in the environment variables');
10+
throw new Error('OPENAI_API_KEY is not defined in the environment variables');
11+
// This will stop the execution of the script
12+
process.exit(1);
13+
14+
}
15+
16+
const configuration = new Configuration({
17+
apiKey: openaiApiKey,
18+
});
19+
// below is the open ai api instance
20+
// The OpenAIApi class is used to interact with the OpenAI API
21+
const openai = new OpenAIApi(configuration);
22+
// Function to generate SQL query using OpenAI API

server/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import express from 'express';
2+
import cors from 'cors';
3+
4+
const app = express();
5+
6+
app.use(cors());
7+
8+
const port = process.env.PORT || 3005;
9+
10+
// endpoint to test the server
11+
app.get("/", (req, res) => {
12+
res.send("Server is running.Hello World from our API");
13+
});
14+
15+
// app.listen is a method that starts the server and listens for incoming requests on the specified port
16+
// The port is either taken from the environment variables or defaults to 3005
17+
// The callback function is executed when the server starts successfully
18+
app.listen(port, () => {
19+
console.log(`Server is running on port ${port}`);
20+
})

0 commit comments

Comments
 (0)