Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 통신
- 앱
- 앱개발
- SwiftUI
- Alamofire
- Swift
- swagger
- 서버개발
- Xcode
- Node.js
- 알고리즘
- JavaScript
- UI
- 개발자
- 백준
- spring boot
- java spring
- 회원가입
- API
- database
- post
- 아이폰
- node
- Java
- spring
- db
- IOS
- 백엔드
- mac
- 개발
Archives
- Today
- Total
YagSill
node.js API 만들기 너무 쉬움 get, post [postman] 사용 본문
728x90
안녕하세요 yagsill 입니다.
간단하게 get, post api를 만들어서 포스트맨으로 사용해 보겠습니다.
일단 폴더 구조 입니다.
폴더구조
app.js
const express = require("express");
const port = 3200;
const router = require("./loaders/routes");
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(router);
app.get("/", (req, res) => {
res.send("BackEnd Server Page");
});
app.listen(port, () => {
console.log("Server Start....");
});
app.js 파일입니다. 파일 빌드 시 제~~~~~일 처음으로 실행되는 스크립트 파일인데요 router를 사용해서 api가 있는 스크립트 파일을
실행해 볼겁니다.
routes.js
const express = require("express");
const router = express.Router();
const web = require("../api/web");
router.use("/api/web", web);
module.exports = router;
저의 라우터 파일입니다.
이제 실제 작동되는 API파일인 web.js를 살펴볼게요
web.js
const express = require("express");
const router = express.Router();
router.get("/", function (req, res) {
return res.json({
resultCode: 200,
resultMsg: "성공",
data: "Web Call Success",
});
});
GET 방식으로 작성되었고 한번 postman으로 호출해보죠
어때요? 너~~~~~무 쉽지 않나요?
그럼 이제 POST방식도 한번 만들어 보죠!
router.post("/", function (req, res) {
var param = {
testParam: req.body.testParam,
};
return res.json({
resultCode: 200,
resultMsg: "POST 성공!",
data: param,
});
});
쉽죠?
저는 파라미터를 하나 넘겨주려고 testParam 이라고 하나 넣어보았습니다.
node.js 에서 param은 req.body 안에 있어요 그래서 포스트맨으로 post요청할때 파라미터 넣어서 보내주면 req.body로 받아야합니다.
그리고 제가 return 값으로 받은 파라미터 값을 그대로 리턴해 주고 있어요(이해하기 쉽도록...)
일단 직접 postman으로 호출해 볼게요!
짠. key 값에 testParam 넣어주고 value에 GOOD이라는 문자를 넣어 봤씁니다.
이전에 제가 return 값을 그대로 보내준다고 했잖아요? 그랬더니 data에 보냈던 파라미터를 그대로 리턴해 준게 확인 되었습니다!
너무 쉽죠??
728x90
'node.js' 카테고리의 다른 글
node.js API multer 이미지 업로드 + [postman 사용] (0) | 2023.06.15 |
---|---|
node.js crypto-js로 비밀번호 암호화 복호화 하기 (0) | 2023.06.13 |
node.js JWT 생성하기 [postman] 사용 (0) | 2023.06.13 |
node.js mybatis-mapper + mysql 연동하기 DB 연동 (0) | 2023.06.11 |
node.js Express 웹 서버 구동하기 (0) | 2023.06.11 |