const hallo = completion.choices[0].message.content; // 응답을 hallo 변수에 저장
console.log(hallo); // 응답을 콘솔에 출력
return hallo; // hallo 값을 반환
} catch (error) {
console.error("Error with OpenAI API:", error.response ? error.response.data : error.message);
throw new Error('Failed to get response from OpenAI API');
}app.use(cors()); // CORS 설정 추가
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// 기본 경로 처리 (서버 상태 확인용)
app.get('/', (req, res) => {
res.send('Server is running!');
});
// POST 요청에 대한 처리
app.post('/hallo', async (req, res) => {
const userMessage = req.body.message; // 프론트엔드에서 보낸 메시지 가져오기
try {
const hallo = await getChatCompletion(userMessage); // ChatGPT 응답을 받아 hallo에 저장
res.json({ response: hallo }); // hallo 값을 JSON 형식으로 프론트엔드에 전달
} catch (error) {
res.status(500).json({ error: 'Error occurred while fetching the response.' });
}
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`); // 서버 실행 확인 메시지
}); const hallo = completion.choices[0].message.content; // 응답을 hallo 변수에 저장
console.log(hallo); // 응답을 콘솔에 출력
return hallo; // hallo 값을 반환
} catch (error) {
console.error("Error with OpenAI API:", error.response ? error.response.data : error.message);
throw new Error('Failed to get response from OpenAI API');
}// 정적 파일 경로 설정
const frontendPath = path.join(__dirname, '../frontend');
app.use(express.static(frontendPath));
// 기본 경로에서 index.html 제공
app.get('/', (req, res) => {
res.sendFile(path.join(frontendPath, 'index.html'));
});
// POST 요청에 대한 처리
app.post('/hallo', async (req, res) => {
const userMessage = req.body.message; // 프론트엔드에서 보낸 메시지 가져오기
try {
const hallo = await getChatCompletion(userMessage); // ChatGPT 응답을 받아 hallo에 저장
res.json({ response: hallo }); // hallo 값을 JSON 형식으로 프론트엔드에 전달
} catch (error) {
res.status(500).json({ error: 'Error occurred while fetching the response.' });
}
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`); // 서버 실행 확인 메시지
});