forked from render-examples/express-hello-world
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
43 lines (40 loc) · 1.25 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
const express = require('express');
const axios = require('axios');
const app = express();
app.use(express.json());
app.get('/', (req, res) => {
res.send(`
<form action="/generate" method="post">
<label for="inputText">Enter text:</label><br>
<input type="text" id="inputText" name="inputText"><br>
<input type="submit" value="Generate Image">
</form>
<img id="outputImage" src="" alt="Generated image will appear here">
`);
});
app.post('/generate', async (req, res) => {
const inputText = req.body.inputText;
try {
const response = await axios.post('https://api.openai.com/v1/images/generations', {
prompt: inputText,
size: "1024x1024",
quality: "standard",
n: 1
}, {
headers: {
'Content-Type': 'application/json',
'Authorization': 'API KEY'
}
});
res.send(`
<img src="${response.data.data[0].url}" alt="Generated image">
<a href="/">Back</a>
`);
} catch (error) {
console.error(error);
res.send('Error generating image.');
}
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});