안녕하세요. 유헤입니다.
node.js에서 curl을 처리해야하는 일이 생겼는데,
이 부분을 어떻게 처리해야할지 난감 했습니다.
java의 경우부터 조사했을때,
https://okky.kr/article/290007
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html#d5e80
https://cafe.naver.com/javacircle/21903
등 HttpClient를 사용하길 권장하고 있었습니다.
그래서 java로 교체를 해야하는건가.. 고민을 하고 있던차에!!
(1) ajax
혹시, ajax 형태로 요청하면 가능할까 싶어서 자료를 조사해보았습니다.
> 실제 Curl을 ajax 형태로 변경할수 있는지에 대한 문의와 답변 <
https://stackoverflow.com/questions/20155531/converting-curl-cmd-to-jquery-ajax
jade 파일에서 아래와 같이 설정을 했으나, 실제로 동작하지 않았습니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | $.ajax({ url: "http://(ip:port)/us_index3/_search", type: 'POST', dataType: 'json', contentType: 'application/json', processData: false, data: result, success: function(data){ alert("data"+data); var postRst = {data:data}; post_to_url("(elasticsearch-url)/search/elkyj",postRst); }, error: function(xhr, ajaxOptions, thrownError){ alert(thrownError); } }); | cs |
데이터나 설정 자체의 문제 같진 않아서 jade가 아닌 node.js 쪽에서 시도해보았습니다.
https://stackoverflow.com/questions/28882411/my-rest-api-works-with-curl-but-fails-in-ajax
https://github.com/edent/Renault-Zoe-API/issues/2
(2) node.js 모듈 request 활용
curl을 python, node.js, php, R, go 언어로 변경해주는 사이트를 찾았습니다.
https://curl.trillworks.com/#node
위와 같이 elasticSearch query 형태의 curl을 node.js로 변환해주는 변환 사이트가 있습니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | var request = require('request'); var headers = { 'Content-Type': 'application/json' }; var dataString = ' { "query" : { "bool" : { "should" : [ { "span_near": { "clauses": [ {"span_term": {"TIE": "car" }}, {"span_term": {"TIE": "hybrid" }} ], "slop": 2147483647, "in_order": false}}]}}}'; var options = { url: 'http://(IP주소):(포트)/us_index3/_search?&scroll=100m&size=200', method: 'POST', headers: headers, body: dataString }; function callback(error, response, body) { if (!error && response.statusCode == 200) { console.log(body); } } request(options, callback); | cs |
이런형태로 적용 할 수 있습니다.
'배우고 있습니다 > Node.js 개발' 카테고리의 다른 글
[node.js 활용기] 웹 크롤링 준비 (2) 에러편 (0) | 2018.12.11 |
---|---|
[node.js 활용기] 웹 크롤링 준비 (1) 기본편 (0) | 2018.12.11 |
[Node.js 개발일지] node.js 전역 변수, global 사용 (0) | 2018.12.06 |
[Node.js 개발일지] java / node-gyp 설치 오류 (0) | 2018.12.06 |
[Node.js 개발일지] node js 버전 번경 (업그레이드) (0) | 2018.12.06 |