방명록
- [Node.js][유튜브 강의 노트] 개발자의품격_Node.js에서 API 서버 만들기2021년 11월 28일 01시 22분 58초에 업로드 된 글입니다.작성자: DandyNow728x90반응형
학습동기
"한시간만에 끝내는 Node.js 입문" 학습 후 추가적인 내용을 학습하게 되었다.
학습내용
1. 강의영상
2. 학습 중 작성한 코드
// api.js // "한시간만에 끝내는 Node.js입문" 학습 후 생성된 폴더에 "api.js" 파일 생성하여 학습 진행함. const express = require('express'); const app = express(); const uuidAPIKey = require('uuid-apikey'); const server = app.listen(3001, () => { console.log('Start server : localhost:3001'); }); // console.log(type); // console.log(uuidAPIKey.create()); // key 생성 후 복붙 const key = { apiKey: 'V1J0JZ9-B5H4Z1Q-N2E32SB-24TA61Y', uuid: 'd864097d-5962-4f86-a89c-31651134a307' }; app.get('/api/users/:apikey/:type', async (req, res) => { // res.send('connect.'); let { apikey, type } = req.params; if (!uuidAPIKey.isAPIKey(apikey) || !uuidAPIKey.check(apikey, key.uuid)) { res.send('apikey is not valid.'); } else { if (type == 'seoul') { let data = [ { name: "홍길동", city: "seoul" }, { name: "김철수", city: "seoul" }, ]; res.send(data); } else if (type == 'jeju') { let data = [ { name: "박지성", city: "jeju" }, { name: "손흥민", city: "jeju" }, ]; res.send(data); } else { res.send('Type is not correct.') } } }); app.get('/api/sales/:apikey/:year', async (req, res) => { let { apikey, year } = req.params; if (!uuidAPIKey.isAPIKey(apikey) || !uuidAPIKey.check(apikey, key.uuid)) { res.send('apikey is not valid.'); } else { if (year == '2019') { let data = [ { product: "반도체", amount: 3928840000 }, { product: "냉장고", amount: 28840000 }, ]; res.send(data); } else if (year == '2020') { let data = [ { product: "반도체", amount: 928840000 }, { product: "냉장고", amount: 840000 }, ]; res.send(data); } else { res.send('year is not correct.') } } });3. uuid-apikey 패키지 설치
https://www.npmjs.com/package/uuid-apikey
uuid-apikey
A Base32-Crockford encoded API Key generator, validator, and converter to turn UUIDs into human readable API Keys
www.npmjs.com
C:\workspace\nodejs>npm install uuid-apikey
해당 패키지를 적용하여 key를 가진 경우에만 api에 접근 할 수 있도록 제한한다.
4. 서버실행
C:\workspace\nodejs>node api.js
5. postman이용한 GET 테스트
Postman API Platform | Sign Up for Free
Postman is an API platform for building and using APIs. Postman simplifies each step of the API lifecycle and streamlines collaboration so you can create better APIs—faster.
www.postman.com
Postman 사용을 위해서는 회원가입이 필요하다.
GET선택 된 상태에서 http://localhost:3001/api/sales/V1J0JZ9-B5H4Z1Q-N2E32SB-24TA61Y/2019를 SEND
학습소감
Node.js에서 API를 적용하는 방법을 간단하게 학습 할 수 있었다.
uuid-apikey 적용 방법도 배울 수 있었다.
API는 GET, POST, PUT, DELETE 메소드를 사용하는데,
이번 강의는 GET을 이용한 API적용의 맛을 살짝 보여준다.
728x90반응형'언어·프레임워크 > Node.js' 카테고리의 다른 글
[Node.js] Swagger을 이용한 API 문서 생성 (0) 2024.01.24 [Node.js][Trouble Shooting] sqlMessage: "Access denied for user 'root'@'localhost' (using password: NO)" (0) 2022.10.08 [Node.js] [문제해결] Vue.js에서 마이크로소프트 액세스 mdb 파일을 읽어 오려다가... SyntaxError (0) 2022.05.25 [Node.js][유튜브 강의 노트] 개발자의품격_한시간만에 끝내는 Node.js 입문 #2 (0) 2021.11.22 [Node.js][유튜브 강의 노트] 개발자의품격_한시간만에 끝내는 Node.js 입문 #1 (0) 2021.11.22 다음글이 없습니다.이전글이 없습니다.댓글