일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- SBT표준
- ambiguous function description
- 스마트컨트렉트 예약어 함수이름 중복
- ethers typescript
- 머신러닝기초
- 티스토리챌린지
- 러스트 기초 학습
- Vue
- 러스트 기초
- ethers websocket
- rust 기초
- 오블완
- 체인의정석
- 스마트컨트렉트테스트
- 컨트렉트 배포 자동화
- 컨트렉트 동일한 함수이름 호출
- chainlink 설명
- 계정추상화
- ethers
- vue기초
- erc4337
- multicall
- Vue.js
- ethers v6
- git rebase
- 러스트기초
- 스마트컨트렉트 함수이름 중복 호출
- 스마트 컨트렉트 함수이름 중복
- erc4337 contract
- ethers type
- Today
- Total
체인의정석
fetch 사용 본문
fetch는 XMLHttpRequest 형태의 API를 노드에서 직접적으로 받아서 쓸 때 사용한다.
다음과 같은 코인 가격정보를 가져오는 예시에서 사용하였다.
observablehq.com/@jflatow/compound-price-feed
Compound Price Feed
Prices Current Current prices on : Percentage deviation in on-chain prices (from reported prices): Historical Price vs Block* Events Posters Anyone may post, this one is run by Compound: World Appendix
observablehq.com
Motivation
Instead of implementing XMLHttpRequest in Node.js to run browser-specific Fetch polyfill, why not go from native http to fetch API directly? Hence, node-fetch, minimal code for a window.fetch compatible API on Node.js runtime.
See Matt Andrews' isomorphic-fetch or Leonardo Quixada's cross-fetch for isomorphic usage (exports node-fetch for server-side, whatwg-fetch for client-side).
먼저 fetch를 설치 해준다.
npm install node-fetch --save
www.npmjs.com/package/node-fetch
node-fetch
A light-weight module that brings window.fetch to node.js
www.npmjs.com
아래 블로그를 통해 fetch를 받아올 때는 다음과 같은 형태로 json으로 한번 바꿔주어야 사용할 수 있다는 것을 알게 되었다.
wooooooak.github.io/javascript/2018/11/25/fetch&json()/
fetch사용시 유의 사항 (json() 함수 사용하기) · 쾌락코딩
fetch사용시 유의 사항 (json() 함수 사용하기) 25 Nov 2018 | javascript network basic js 개발자들은 network request 요청이 필요할 경우 대부분 axios.js나 기타 다른 라이브러리를 쓰는 것 같다. js에서 기본적으
wooooooak.github.io
다음은 고친 코드의 결과이다.
const fetch = require('node-fetch');
.
.
.
// reports = (await fetch('https://prices.compound.finance')).json()
fetch('https://prices.compound.finance')
.then((res) => {
return res.json(); //Promise 반환
})
.then((json) => {
console.log(json); // 서버에서 주는 json데이터가 출력 됨
});
'개발' 카테고리의 다른 글
자바스크립트와 전략패턴 (0) | 2021.06.28 |
---|---|
node.js, json 형태로 요청하기 (0) | 2021.03.17 |
MacOS - Express generator 인식이 안될 경우 (0) | 2021.01.26 |
블록체인 공증 모듈 - 게시판 기능 만들기, 양쪽의 동의를 얻은 후 블록체인에 데이터 저장시키기 (0) | 2021.01.07 |
게시판 조회 화면 상황에 따라 다르게 띄우기 (오류 수정) (0) | 2021.01.05 |