- 날씨 api 적용
-postman 사이트 사용
- 마무리
import axios from "axios"; import { useState } from "react"; const Chat = () => { const [question, setQuestion] = useState(""); const [content, setContent] = useState(""); const onSubmitChat = async (e) => { try { e.preventDefault(); const response = await axios.post( "https://holy-fire-2749.fly.dev/chat", { // question: `${question}`, // question: question, question, }, { headers: { Authorization: "Bearer BLOCKCHAINSCHOOL3", }, } ); if (response.status !== 200) { alert("오류가 발생했습니다."); return; } console.log(response); setContent(response.data.choices[0].message.content); } catch (error) { console.error(error); } }; return ( <div className="bg-black flex flex-col min-h-screen flex justify-center items-center text-white"> <form onSubmit={onSubmitChat}> <input className="text-black" type="text" value={question} onChange={(e) => setQuestion(e.target.value)} /> <input type="submit" value="검 색" /> </form> {content && <div className="mt-4 px-16">{content}</div>} </div> ); }; export default Chat;
express
- package json 만들어주기
{ "name": "express", "version": "1.0.0", "description": "", "main": "app.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "zzeongho", "license": "MIT", "dependencies": { "express": "^4.18.2" } }
- app.js 만들어주기
const express = require("express"); const app = express(); const port = 3010; app.get("/abc", (req, res) => { res.send("ABC Request!!"); }); app.post("/", (req, res) => { res.send("This is Post Request"); }); app.put("/", (req, res) => { res.send("This is Post Request"); }); app.delete("/", (req, res) => { res.send("This is Post Request"); }); app.listen(port, () => { console.log(`Server listening on port ${port}`); });
- .gitignore 에는 꼭 .env와 node_modules 는 기계적으로 넣어주자.