-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
46 lines (37 loc) · 1.12 KB
/
app.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
require("dotenv").config();
require("express-async-errors");
const express = require("express");
const app = express();
const port = process.env.PORT || 3100;
const aiCall = require("./openai");
const notFoundMiddleware = require("./middleware/not-found");
const errorMiddleware = require("./middleware/error-handler");
//Middleware
app.use(express.static("public"));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.get("/", (req, res) => {
res.status(200).send("<h1>Home Page</h1>");
});
// The user request prompt
app.post("/palette", async (req, res) => {
const userPrompt = req.body.userPrompt;
if (userPrompt) {
// The OpenAI completion call
const aiResponse = await aiCall(userPrompt);
// Parse the openai response into an array object
const response = {
colors: JSON.parse(aiResponse),
};
res.status(200).json(response);
return;
// Return array object of colors
} else {
console.log("NO PROMPT RECEIVED");
}
});
app.use(notFoundMiddleware);
app.use(errorMiddleware);
app.listen(port, () => {
console.log(`Server running on port ${port}...`);
});