...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
const express = require('express'); const axios = require('axios'); const bodyParser = require('body-parser'); const DALLE_URL = ''; // API URL ex) 'https://api.openai.com/v1/images/generations' const DALLE_API_KEY = ''; // API KEY const app = express(); const router = express.Router(); router.post('/request', (request, response) => { const bodyData = request.body; requestDallerequestDALLEServer(response, bodyData) .then(() => response.end()) .catch((error) => response.status(error.status).json(error).end()); }); /** * DALL·E 서버로 요청을 합니다. * @param {Object} body 전송할 Data Object * @param {Response} res 클라이언트 응답 객체 * @returns {Promise} */ function requestDALLEServer(res, body) { const source = axios.CancelToken.source(); const url = DALLE_URL; return new Promise((resolve, reject) => { try { axios.post(url, body, { headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${DALLE_API_KEY}` }, responseType: 'stream', cancelToken: source.token }).then(response => { response.data.pipe(res); }).catch(error => { reject({ status: error.response ? error.response.status : 500, message: error.message }); }); res.on('close', () => { source.cancel('요청을 취소합니다.'); resolve(); }); } catch (e) { reject(e); } }); } app.use(bodyParser.json()); app.use('/', router); app.listen(8080); |
...