-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
56 lines (44 loc) · 1.37 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const express = require('express');
const cors = require('cors');
const todosRouter = require('./routes/todos');
const openaiRoutes = require('./routes/openai');
const app = express();
const PORT = 3000;
// Setting CORS to allow chat.openapi.com is required for ChatGPT to access your plugin
app.use(cors({ origin: [`http://localhost:${PORT}`, 'https://chat.openai.com'] }));
app.use(express.json());
// Simple request logging to see if your plugin is being called by ChatGPT
app.use((req, res, next) => {
console.log(`Request received: ${req.method}: ${req.path}`)
next()
})
// OpenAI Required Routes
app.use(openaiRoutes);
// The dummy todos API
app.use('/todos', todosRouter);
// Proxy to an existing API
const api_url = 'http://localhost';
app.all('/:path', async (req, res) => {
const { path } = req.params;
const url = `${api_url}/${path}`;
console.log(`Forwarding call: ${req.method} ${path} -> ${url}`);
const headers = {
'Content-Type': 'application/json',
};
try {
const response = await axios({
method: req.method,
url,
headers,
params: req.query,
data: req.body,
});
res.send(response.data);
} catch (error) {
console.error(`Error in forwarding call: ${error}`);
res.status(500).send('Error in forwarding call');
}
});
app.listen(PORT, () => {
console.log(`Plugin server listening on port ${PORT}`);
});