import Bean from '../decorators/Bean' import http from 'http'; import url from 'url'; export enum PING_RESULT_TYPE { ERROR, SUCCEEDS = 1 } export interface PING_RESULT { result: PING_RESULT_TYPE, e?: any } export class HttpRestConnection { constructor() { } public ping = (_url: string): Promise => { //RequestOptions | string | URL let urlObj; try { urlObj = url.parse(_url); } catch (err) { return Promise.reject({result: PING_RESULT_TYPE.ERROR, e: new Error(" url parse failed ")}); } let options = { hostname: urlObj.hostname, port: urlObj.port, timeout: 1000, method: 'POST', } return new Promise((resolve, reject) => { try { let req = http.request(options, function (res) { let serverData = ''; // console.log(res.statusCode) resolve({result:PING_RESULT_TYPE.SUCCEEDS}) // 404 같은건 여기로 에러가 떨어진다 // stream 형 // res.on('data', function (chunk) { // // console.log("=========================== http data") // // console.log(chunk) // serverData += chunk; // }); // res.on('end', function (res:any) { // // console.log("=========================== http end") // // // console.log(res); // // resolve({result:PING_RESULT_TYPE.SUCCEEDS}) // }); }) .on('error', function (e) { // console.log("=========================== http error") // on(event: 'error', listener: (err: Error) => void): this; reject({result: PING_RESULT_TYPE.ERROR, e: e}) }) .on('timeout', () => { // console.log("=========================== http timeout") req.abort(); reject({result: PING_RESULT_TYPE.ERROR, e: "timeout"}) }).end(); } catch (e) { reject({result: PING_RESULT_TYPE.ERROR, e: e}) } }) } }