카테고리 없음
[09.27]
문앵
2021. 9. 27. 10:25
데몬1 실행 후 클라이언트)
PS C:\Program Files\INGcoin\daemon> .\ingcoin-cli.exe -rpcport=3000 -rpcuser=ingoo -rpcpassword=1234
getnewaddress ingoo4
⬇
iDRG7emsdnddsSuojhTASvFnLeCouKw6fY
(새로운 주소를 생성)
이제 http 요청을 해볼것임
curl 이용해서 (이때는 리눅스를 이용해야 함)
새로운 탭 생성-> wls 환경설정
curl [url 주소]
# curl
- 얘는 shell 이기때문에 리눅스 환경에서 돌려야 함
옵션값 존재
-H : header
-d : data (바디 내용 값)
-X : request method
curl [option][도메인]
ex> curl -X POST -H "Content-type:application/json" -d "{}" http://naver.com
(Content-type:application/json ▶ 데이터타입 제이슨 형태라는 뜻 )
[도메인] = RPCUSER:RPCPASSWORD@IPADDRESS:RPCPORT
RPCUSER : ingoo
RPCPASSWORD : 1234
IPAADDRESS : 127.0.0.1
RPCPORT : 3000
=> ingoo:1234@127.0.0.1:3000 = [도메인] 이 완성된다!
{ 데이터는 요런 식으로
"method" : "getnewaddress","params" : ["ingoo5"]
}
그래서 정리해보면
curl [option] [도메인]
-> curl -X POST -H "Content-type:application/json"
-d "{"method":"getnewaddress","params":["ingoo5"]}" ingoo:1234@127.0.0.1:3000
wsl 에서 해당 명령어를 입력했더니
curl: (7) Failed to connect to 127.0.0.1 port 3000: Connection refused
라는 오류가 떴다.
포트 3000번이 돌아가지 않는다는 의미라고 한는데 실제로 로컬에서 localhost:3000쳤더니
아무것도 뜨지않음
- 나는 데몬 돌리면 되는건줄 알았는데 (3000번 포트라고 지정해놓은거..) 왜 안되는걸까..?
아무튼 http 요청을 node.js에서 해본다고 한다.
vs코드를 열고 환경 구축하기
server.js
npm init
npm install express
+ npm install request
패키지 설치하기
<server.js>
const express = require('express')
const app = express();
const request = require('request') //기본적으로 함수임
app.get('/',(req,res)=>{
res.send("hello incoin!")
})
app.get('/naver',(req,res)=>{
// res.send("http://www.naver.com")
//요청 request
// request 2개의 인자값 존재
// 1: url 값 or object { uel 존재합니다 }
// 2 : 콜백에 대한 값
request(`https://naver.com`,(err,response,body)=>{
console.log(body)
})
res.send('naver')
})
app.listen(3800,()=>{
console.log(`server start port3800`)
})
현재 콘솔창에 body를 찍었으므로
로컬창에
127.0.0.1:3800/naver 라고 요청시 터미널에 naver 정보가 html 형태로 찍혀야 함
아 리퀘스트 설명해주셨는데 설명 제대로 못들은
10:40분? 그쯤부터
-----------------------------------
app.get('/naver2',(req,res)=>{
request({
url:"http://naver.com",
method:"POST",
headers:{"Content-type":'application/json'},
body:"{msg:'hello world!'}"
}
,(err,response,data)=>{
console.log(data)
res.send("naver2")
})
})
이런식으로도 처리 가능
(네이버에서 포스트로 응답을 주는 경우는 없어서 실제로 콘솔에 body 찍어도 별내용이 뜨진 않음)
반응형