[ node js ]

   조회 4535   추천 0    

https://gist.github.com/jxson/963339/33a0623a000334aa0ab97def069c3902a1212f6c


해당 URL을 참조하여, 아래의 js를 실행하였으나, 에러가 발생하네요
해당 문젬점 해결을 위하여, 조언 부탁 드려 봅니다.
감사합니다.

 

----- 실행 화면 (에러 발생)

❯ bitly http://www.naver.com
/opt/homebrew/bin/bitly.js:3
        link = process.ARGV[2],
                           ^ # <======================================== !!!

TypeError: Cannot read properties of undefined (reading '2')
    at Object.<anonymous> (/opt/homebrew/bin/bitly.js:3:22)
    at Module._compile (node:internal/modules/cjs/loader:1218:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1272:10)
    at Module.load (node:internal/modules/cjs/loader:1081:32)
    at Module._load (node:internal/modules/cjs/loader:922:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:82:12)
    at node:internal/main/run_main_module:23:47

Node.js v19.3.0

----- 전체 코드

❯ \cat /opt/homebrew/bin/bitly.js
var request = require('request'),
        sys = require('sys'),
        link = process.ARGV[2],
        bitly = 'http://api.bit.ly/v3/shorten';

function urlencode(str) {
return escape(str)
    .replace('+', '%2B')
    .replace('%20', '+')
    .replace('*', '%2A')
    .replace('/', '%2F').replace('@', '%40');
}

// prepend http if it does not exist.
if (!/^https?:\/\//i.test(link)) {
    link = 'http://' + link;
}

// urlencode the link and add to params.
var params = 'login=YOUR_LOGIN_ID&apiKey=YOUR_API_KEY&longUrl='+urlencode(link)+'%2F&format=json';

request({uri:bitly, body:params, method:'POST'},function(error,response,body){
    if(!error && response.statusCode === 200 && body)
    {
        var json = JSON.parse(body);
        console.log(json.data.url);
    }
});

짧은글 일수록 신중하게.
김제연 2023-02
소문자 입니다 ARGV -> argv
조언 감사합니다.

알려 주신 조치대로 하여 기존의 문제점은 발생하지 않으나,
  의도한 결과 값이 아닌 다른 값이 출력이 되서요
❯ bitly www.google.com

❯ undefined # <======== cltr + v


메모장에 clipboard에 저장된 결과 값을 복사를 해보면,
undefined
이런 값을 찍고 있기는 합니다. 꾸벅 ~.~


QnA
제목Page 2121/5718
2014-05   5209528   정은준1
2015-12   1741072   백메가
2021-01   4533   달리는붐봄
2023-07   4534   남경림
2022-03   4534   진신두
2016-11   4534   Sunrise
2016-01   4534   엠브리오
2018-12   4534   김건우
2007-07   4534   유호준
2015-09   4534   박철
2014-08   4534   윈도우10
2017-02   4534   혀기
2016-07   4534   Win31
2015-01   4534   슬루프
2014-08   4534   김건우
2006-11   4534   김승룡
2015-05   4534   이천풍
2016-05   4534   박성만
2019-01   4534   화란
2016-10   4534   막판대장
2017-01   4534   쿰척쿰척
2016-01   4534   이현상