-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
43 lines (34 loc) · 1.17 KB
/
server.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
const express = require('express');
const path = require('path');
const openai = require("./openai");
const rateLimit = require('express-rate-limit');
global.fetch = require("node-fetch");
// Rate limiter from express-rate-limiter
const limiter = rateLimit({
windowMs: 24 * 60 * 60 * 1000, // 24 hrs in milliseconds
max: 30,
message: 'You have exceeded the 30 requests in 24 hrs limit!',
headers: true,
});
const app = express();
// Run a https server instead of the default http server
const http = require("http").createServer(app);
// Serve static files of the react application
app.use(express.static(path.join(__dirname, 'web/build')));
app.use(express.json());
app.use(express.urlencoded());
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config();
}
// Primary api endpoint with rate limiting enabled
app.post(
"/api/completions",
limiter,
async (req, res) => res.send(await openai.translate(req.body.plaintext))
);
// Catch all other routes and throw the webpage to the user
app.get(
'*',
(req, res) => res.sendFile(path.join(__dirname, '/web/build/index.html'))
);
http.listen(process.env.PORT || 5000, () => console.log("Running."));