...
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; requestDALLEServer(response, bodyData) .then((data) => response.endjson(data)) .catch((error) => response.status(error.status).json(error).end()); }); /** * DALL·E 서버로 요청을 합니다. * @param {Response} response * @param {Object} bodyData * @returns {Promise} */ function requestDALLEServer(response, bodyData) { const source = axios.CancelToken.source(); const url = DALLE_URL; return new Promise((resolve, reject) => { try { axios.post(url, bodyData, { headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${DALLE_API_KEY}` }, cancelToken: source.token }).then(responseres => { response.jsonresolve(responseres.data); }).catch(error => { reject({ status: error.response ? error.response.status : 500, message: error.message }); }); response.on('close', () => { source.cancel('요청을 취소합니다.'); resolve(); }); } catch (e) { reject(e); } }); } app.use(bodyParser.json()); app.use('/', router); app.listen(8080); |
...