-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
40 lines (32 loc) · 841 Bytes
/
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
import * as dotenv from "dotenv";
import express from "express";
import cors from "cors";
import { Configuration, OpenAIApi } from "openai";
dotenv.config();
const config = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(config);
const app = express();
app.use(cors());
app.use(express.json());
app.post("/api/photo", async (req, res) => {
try {
const prompt = req.body.prompt;
const response = await openai.createImage({
prompt,
n: 1,
size: "1024x1024",
});
const image = response.data.data[0].url;
res.send({ image });
} catch (error) {
console.log(error);
res
.status(500)
.send(error?.response.data.error.message || "Something went wrong");
}
});
app.listen(8080, () => {
console.log("Server started on port 8000");
});